AFNetworking 2.0 cancel specific task

前端 未结 3 629
伪装坚强ぢ
伪装坚强ぢ 2021-02-01 21:45

I am trying out afnetworking 2.0 and just trying to figure out how to cancel specific tasks. The old way would be to use something like

[self cancelAllHTTPOperat         


        
3条回答
  •  孤城傲影
    2021-02-01 22:08

    No need to save it, here is my implementation, use your subclass of AFURLSessionManager for cancelling specific request:

    - (void)cancelAllHTTPOperationsWithPath:(NSString *)path
    {
        AFURLSessionManager * yourSessionManager = [self getSessionManager];
    
        [[yourSessionManager session] getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
            [self cancelTasksInArray:dataTasks withPath:path];
            [self cancelTasksInArray:uploadTasks withPath:path];
            [self cancelTasksInArray:downloadTasks withPath:path];
        }];
    }
    
    - (void)cancelTasksInArray:(NSArray *)tasksArray withPath:(NSString *)path
    {
        for (NSURLSessionTask *task in tasksArray) {
            NSRange range = [[[[task currentRequest]URL] absoluteString] rangeOfString:path];
            if (range.location != NSNotFound) {
                [task cancel];
            }
        }
    }
    

提交回复
热议问题