I migrated my networking functionality from AFNetworking
to AFNetworking v2
and instead of AFHttpClient
I am using AFHTTPRequestOperationManager
to support iOS6 as well.
My issue is that while in AFHttpClient
there was the functionality to cancel a pending request using the
- (void)cancelAllHTTPOperationsWithMethod:(NSString *)method path:(NSString *)path;
method, in the AFHTTPRequestOperationManager
there is no such obvious method.
What I've done up to now is subclassing AFHTTPRequestOperationManager
and declaring an iVar
AFHTTPRequestOperation *_currentRequest;
When I make a request the code is something like
- (void)GetSomething:(NSInteger)ID success:(void (^)(MyResponse *))success failure:(void (^)(NSError *))failure
{
_currentRequest = [self GET:@"api/something" parameters:@{@"ID": [NSNumber numberWithInteger:ID]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
MyResponse *response = [MyResponse responseFromDictionary:responseObject];
success(response);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failure(error);
}];
}
and I have a
- (void)cancelCurrentRequest;
methods which all that does is
- (void)cancelCurrentRequest
{
if(_currentRequest) {
[_currentRequest cancel]; _currentRequest = nil;
}
}
Now, I don't think this is good practice and when the method is called I get (NSURLErrorDomain error -999.)
which is why I need some advice on getting this done correctly.
Thank you in advance.
Objective-C
[manager.operationQueue cancelAllOperations];
Swift
manager.operationQueue.cancelAllOperations()