AFNetworking 2: How to cancel a AFHTTPRequestOperationManager request?
You don't have to subclass AFHTTPRequestOperationManager
, because when you send request, AFHTTPRequestOperation
returns from the
- (AFHTTPRequestOperation *)GET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
simply save it somewhere or make static and then perform cancel
when the request need to be canceled.
Example:
- (void)sendRequestToDoSomething
{
static AFHTTPRequestOperation *operation;
if(operation) //cancel operation if it is running
[operation cancel];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//configure manager here....
operation = [manager GET:urlString parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//do something here with the response
operation = nil;
} failure:^(AFHTTPRequestOperation *op, NSError *error) {
{
//handle error
operation = nil;
}
Objective-C
[manager.operationQueue cancelAllOperations];
Swift
manager.operationQueue.cancelAllOperations()