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) {
}
];
}
Related
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];
}];
}
I need to send all my forms on server, firstly text forms and then images. Do someone working with it?
The documentation page of AFNetworking shows the following example:
Multi-Part Request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"foo": #"bar"};
NSURL *filePath = [NSURL fileURLWithPath:#"file://path/to/image.png"];
[manager POST:#"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:#"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Success: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
I tried to make GET HTTP response. I need to get the html code for the subsequent parsing, but responseObject is nil.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager GET:#"http://www.example.com/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSError *error;
HTMLParser *parser = [[HTMLParser alloc] initWithString:responseObject error:&error];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
For get html code we will need to build a custom response serializer to decode the NSData response from the web server into a NSString. We will need to subclass AFHTTPResponseSerializer and implement the following method:
- (id)responseObjectForResponse:(NSURLResponse *)response
data:(NSData *)data
error:(NSError *__autoreleasing *)error
{
return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
Why for example you have not use this solution below instead of subcalssing. It does the same thing, but you don't need to create additional files, just for overload one method.
So you can just add encoding your responseObjet in the block for example, and it will work as well. I am using POST in my example but it should work with GET in the same way but without parameters, but idea of the just conversation.
+ (void)makeRequestWithParams:(NSDictionary *)params
success:(OperationCompletionBlock)success
failure:(OperationCompletionBlock)failure
{
NSString *path = #"http://www.example.com/";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFCompoundResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/html"];
[manager POST:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString* encodedString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(#"%#", encodedString);
success(nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
failure(nil);
}];
}
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/
In RestKit Version 0.10.x I used
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:#"/special/path" usingBlock:^(RKObjectLoader *loader) {
loader.objectMapping = aMapping;
loader.delegate = _delegate;
loader.method = RKRequestMethodPOST;
loader.userData = [MobileGatewayReauthentication class];
loader.params = [anDictionary toJsonRKParam];
}];
to add a NSDictionary to loader.params to send a collection of parameters as the HTTP body of the request.
Since RestKit 0.20.0 the method loadObjectsAtResourcePath has been replaced by the following method, where it is no longer possible to pass a NSDictionary in parameters, which is used as HTTP Body (JSON encoded):
[RKObjectManager.sharedManager getObjectsAtPath:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
NSLog(#"success");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"failure");
}];
When working with [[RKObjectManager sharedManager].HTTPClient (AFNetworking) it works like a charm when setting:
[[RKObjectManager sharedManager].HTTPClient setParameterEncoding:AFJSONParameterEncoding];
[[RKObjectManager sharedManager].HTTPClient postPath:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Response: %#", operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#",error.localizedDescription);
}];
But I need the higher-level object mapper.
[[RKObjectManager sharedManager].HTTPClient setParameterEncoding:AFJSONParameterEncoding];
has no effect on [RKObjectManager sharedManager] but
[[RKObjectManager sharedManager].HTTPClient setDefaultHeader:#"aHeader" value:#"aValue"];
has. So I assumed that setParameterEncoding works, too.
Is it a bug, is it not yet implemented, or have I missed something?
Thanks a lot.
I really have missed something.
loadObjectsAtResourcePath get's (for sure) called with RKRequestMethodGET. So the parameters get append to the URL, not to the HTTP body.
To append the parametrs as a JSON HTTP body, you have to use a post method, for example
- (void)postObject:(id)object
path:(NSString *)path
parameters:(NSDictionary *)parameters
success:(void (^)(RKObjectRequestOperation *operation, RKMappingResult *mappingResult))success
failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure;
Excuse the hasty question. I should have seen it before.
If I understood your problem correctly, RestKit creator pushed something in the development branch (which is always tested and pretty safe to use) that might fix your problem (Commit here), which basically sets by default this:
[[RKObjectManager sharedManager].HTTPClient setDefaultHeader:#"Accept" value:RKMIMETypeJSON];
[[RKObjectManager sharedManager].HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
Hope this help!
There could be problem with the path in getObjectsAtPath:path
If you are using slash (/) in the beginning of path, move it end of baseurl as shown in example below.
NSURL *baseURL = [NSURL URLWithString:#"http://rest.example.com:8080/test/"];
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[objectManager getObjectsAtPath:#"rest/updates"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(#"success");
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(#"failure");
}];