Using AFHTTPRequestOperationManager at a later time - objective-c

I'm using AFNetworking for my app.
I want to create a queue mechanism with different priority for each HTTP request.
For that - I need to be able to create an HTTP Request using AFNetowrking but use it later.
The example for creating an HTTP request is:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
This code will send the request immediately.
How can I just create the request (method, parameters, url), but use it at a later time?

Check operationQueue of AFHTTPRequestOperationManager. If you suspend it before adding request, it will not run until you resume operation queue. For example:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.operationQueue setSuspended:YES];

Turns out you need to create an AFHTTPRequestOperation instead of a manager.
Full article here:
http://samwize.com/2012/10/25/simple-get-post-afnetworking/

Related

How to cancel a specific POST request through AFNetworking? [duplicate]

Hi I am making post request using AFnetworking 2.0.
My request looks like this.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
[manager.requestSerializer setValue:#"some value" forHTTPHeaderField:#"x"];
[manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//doing something
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// error handling.
}];
How can i cancel this request???
POST method return the AFHTTPRequestOperation operation. You can cancel it by calling cancel.
AFHTTPRequestOperation *post =[manager POST:nil parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//doing something
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// error handling.
}];
//Cancel operation
[post cancel];
Tried [manager.operationQueue cancelAllOperations] ?

Properly formatting parameters for POST message in AFNetworking?

So I'm trying to use AFNetworking to essentially pull down the same information that I get with the following cURL request in Terminal:
curl --data 'method=my-service.search&document_type=x&keywords=y' http://mywebsite.com/services/json/my-service.search
If I type that into Terminal, I get JSON back. Now I want to essentially do the same thing (download the JSON) so I can parse it in Xcode.
I've tried asking this question in different terms here, but I think I'm narrowing in on why I am getting issues — I'm not properly formatting the parameters.
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:#"document_type", #"keywords", nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
So, how do I perform this task with the right formatting to return proper results? Do I set the urlString to http://mywebsite.com/services/json/my-service.search, should 'method=my-service.search' enter parameters in any form? How do I define the two parameters that I have listed (e.g. set document_type to podcast)?
Sorry for the flurry of questions. I'm just frustrating that this web service works so well in Terminal but I can't apply it to Xcode with the knowledge that I currently have.
Thanks!
I think you have created wrong parameters dictionary, the fowolling code
NSDictionary *parameters = #{"document_type":value, #"keywords":value};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:urlString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
//SUCCESS BLOCK
} failure:^(NSURLSessionDataTask *task, NSError *error) {
//FAILURE BLOCK
}];

Afnetworking wrong block is returned

I am using afnetworking and AFHTTPRequestOperationManager,
I have a singleton class, which contains all my api call. However, when I have concurrent api call, wrong data is being returned. API call A is returning API call B response?
CHAFHTTPRequestOperationManager is a subclass of AFHTTPRequestOperationManager
Anyone experience the same problem, what do I need to do to solve this:
NSString *path = [NSString stringWithFormat:#"users/%#/profile_photo", userName];
CHAFHTTPRequestOperationManager *manager = [CHAFHTTPRequestOperationManager sharedManagerObj];
[manager GET:path
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}
];
}

How to reuse obj-c block?

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://aaa"
success:^(AFHTTPRequestOperation *operation, id responseJSON) {
...
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[[TWMessageBarManager sharedInstance]
showMessageWithTitle:#"Network connection failure"
description:#"Please check your network"
type:TWMessageBarMessageTypeError];
}];
Some block is constant, can be used repeatedly. For example here's failure block, How can I reuse this block for reduce the amount of code?
I hope it is a global reuse, rather than the current context, so I can store it as a property? Or get_method()?
Save the block to a variable, then you can pass that around:
void (^failureBlock)(AFHTTPRequestOperation *operation, NSError *error) = ^void(AFHTTPRequestOperation *operation, NSError *error) { /* write what you want */ };
void (^successBlock)(AFHTTPRequestOperation *operation, id responseJSON) = ^void(AFHTTPRequestOperation *operation, id responseJSON) { /* write what you want */ };
Then you can use it in further calls like this:
[manager GET:#"" success:successBlock failure: failureBlock];
Bonus: Check out this guide.
you can save it like a variable like so:
void(^blockname)(AFHTTPRequestOperation*, NSError*) = ^(AFHTTPRequestOperation *operation, NSError *error) {
[[TWMessageBarManager sharedInstance]
showMessageWithTitle:#"Network connection failure"
description:#"Please check your network"
type:TWMessageBarMessageTypeError];
}
then just put blockname for the failure parameter instead of the whole thing
Another approach, instead of reuse blocks, you should consider reuse the whole function
- (void)getURLPath:(NSString *)urlPath withSuccessBlock:(void (^)(id responseJSON))block {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:urlPath
success:^(AFHTTPRequestOperation *operation, id responseJSON) {
...
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[[TWMessageBarManager sharedInstance]
showMessageWithTitle:#"Network connection failure"
description:#"Please check your network"
type:TWMessageBarMessageTypeError];
}];
}

Can't find file AFJSONRequestOperation.h with AFNetworking and AFOAuth2Client

I am using cocoapods to install AFNetworking and AFOAuth2Client. The issue is that it can't import a header file, AFJSONREquestOperation. I have no idea where this dependency lies. Is it another pod or extension to AFNetworking?
It depends of you installed AFNetworking 2.x version, but AFOAuth2 used 1.x version (the latest 1.x is 1.3.3)
If you used Cocoapods, just write in pod file
pod 'AFNetworking', '1.3.3' and than run "pod install"
#LIAL's answer is correct -- as of right now, AFOAuth2Client is not compatible with AFNetworking 2.0. There is an open pull request https://github.com/AFNetworking/AFOAuth2Client/pull/55 that is supposed to make AFOAuth2Client work with AFNetworking 2.0.
You could fork the repo of mlwelles or use https://github.com/mlwelles/AFOAuth2Client.git by modifying your Podfile for AFOAuth2Client line to look like this:
pod 'AFOAuth2Client', :git => 'git://github.com/mlwelles/AFOAuth2Client.git'
Try importing it like this:
#import <AFNetworking/AFJSONRequestOperation.h>
If you can't that way, please make sure you are using Xcode 5. Happened to me when using Xcode 4.6.3.
AFHTTPRequestOperationManager Based Code
AFNetworking 2.x
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
AFNetworking 3.x
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
AFHTTPRequestOperation Based Code
AFNetworking 2.x
NSURL *URL = [NSURL URLWithString:#"http://example.com/resources/123.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
[[NSOperationQueue mainQueue] addOperation:op];
AFNetworking 3.x
NSURL *URL = [NSURL URLWithString:#"http://example.com/resources/123.json"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
This answer it's helpful for both user who have use AFNetworking 2.x & 3.x
Thanks.