AFNetworking 2: How to cancel a AFHTTPRequestOperationManager request?

后端 未结 2 1275
难免孤独
难免孤独 2020-12-23 20:28

I migrated my networking functionality from AFNetworking to AFNetworking v2 and instead of AFHttpClient I am using AFHTTPRequest

相关标签:
2条回答
  • 2020-12-23 20:38
    [manager.operationQueue cancelAllOperations];
    
    0 讨论(0)
  • 2020-12-23 20:59

    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;
    }
    
    0 讨论(0)
提交回复
热议问题