AFNetworking 2: How to cancel a AFHTTPRequestOperationManager request?

我的未来我决定 提交于 2019-11-29 21:19:37
[manager.operationQueue cancelAllOperations];

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!