AFNetworking 2.0取消了特定任务 [英] AFNetworking 2.0 cancel specific task

查看:124
本文介绍了AFNetworking 2.0取消了特定任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试afnetworking 2.0并试图弄清楚如何取消特定任务。
旧方法是使用类似

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 cancelAllHTTPOperationsWithMethod:@"POST" path:@"user/receipts"]

但我在2.0中看不到这样的东西

but I dont see anything like this in 2.0

我创建了一个 AFHTTPSessionManager 的子类,它允许我访问待处理任务的数组,我可以直接取消它们但我不知道如何识别另外1个任务,所以我只能取消特定的任务。
任务确实有一个taskidentifier,但这似乎不是我需要的。

I created a sub class of AFHTTPSessionManager which gives me access to the array of pending tasks and I can cancel them directly but I dont know how to identify 1 task from another so I can cancel only specific tasks. Task does have an taskidentifier but this doesnt appear to be what I need.

NSString *path = [NSString stringWithFormat:@"user/receipts"];
[self.requestSerializer setAuthorizationHeaderFieldWithUsername:[prefs valueForKey:@"uuid"] password:self.store.authToken];
[self GET:path parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
            completionBlock(responseObject);
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            errorBlock(error);
        }];

现在,如果我想取消此请求,我将如何处理此问题?

now if i wanted to cancel this request only how would I approach this?

推荐答案

您可以将任务存储在变量中,以便日后访问:

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);
        }];

然后只需用 [任务取消] 。

另一种方法是保存任务的任务ID,然后向URL会话询问其任务并确定您要取消的任务:

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];
    }
}

这篇关于AFNetworking 2.0取消了特定任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆