AFNetworking 2.0 cancel specific task

前端 未结 3 690
伪装坚强ぢ
伪装坚强ぢ 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 21:48

    You can store the task in a variable so you can access it later:

    NSURLSessionDataTask* task = [self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
                completionBlock(responseObject);
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                errorBlock(error);
            }];
    

    Then simply cancel it with [task cancel].

    Another way would be to save the task ID of the task and later ask the URL session for its tasks and identify the task you wish to cancel:

    // save task ID
    _savedTaskID = task.taskIdentifier;
    
    // cancel specific task
    for (NSURLSessionDataTask* task in [self dataTasks]) {
        if (task.taskIdentifier == _savedTaskID) {
            [task cancel];
        }
    }
    

提交回复
热议问题