I'm stuck now some time and I need help. So in AFNetworking 2.0 we have AFHTTPRequestOperation
so I could easily use NSOperationQueue
and have some dependencies. So what we have now is only AFHTTPSessionManager
and NSURLSession
that does not subclass NSOperation
. I have class APIClient
that subclasses AFHTTPSessionManager
. I am using that class as singleton as sharedClient
. I have overriden GET and POST so for example GET looks this:
- (NSURLSessionDataTask *)GET:(NSString *)URLString
parameters:(NSDictionary *)parameters
success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure {
NSURLSessionDataTask *task = [super GET:URLString parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
success(task, responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
failure(task, [Response createErrorWithAFNetworkingError:error]);
}];
return task;
}
Do you have any idea how to implement in that manner (if it's possible) to wrap that as NSOperation
? So what I want to do - I want to be able to run in parallel two network calls, and after that have another method call that depends on second network call of first two calls. Do you have any idea what would be best approach?
I've written a quick little set of classes (https://github.com/robertmryan/AFHTTPSessionOperation/) that wrap
AFHTTPSessionManager
requests in asynchronousNSOperation
subclass. You can then use that to enjoymaxConcurrentOperation
constraints, or operation dependencies.For example, here's an example where we issue two concurrent requests and have a completion operation dependent upon completion of both of those requests:
It's worth noting that since you're only dealing with two requests, you could also use dispatch groups to accomplish the same thing:
With dispatch groups, you just need to be careful that every path within both the
success
andfailure
blocks calldispatch_group_leave
.