取消所有操作+ AFNetworking 3.0 [英] Cancel All Operations + AFNetworking 3.0

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

问题描述

我创建了一个继承自AFURLSessionManager的类HTTPServiceProvider。加入下面的代码来获得该数据。

I have created a class HTTPServiceProvider inherited from AFURLSessionManager. Added below code to get the data.

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let manager = AFURLSessionManager(sessionConfiguration: configuration)
let dataTask = manager.dataTaskWithRequest(request) { (response, responseObject, error) in
         //Perform some task
}
dataTask.resume()

我要添加到dataTask由operationQueue提供AFURLSesstionManger并取消在一些其它类中的所有操作(BaseController.swift )再次调用相同的请求之前。

I want to add dataTask to operationQueue provided by AFURLSesstionManger and cancel all the operation in some other class (BaseController.swift) before calling the same request again.

尝试此代码但无法正常工作 -

self.operationQueue.addOperationWithBlock{
     //Added above code
}

$

HTTPServiceProvide.sharedInstance.operationQueue.cancelAllOperations

但它不起作用:(

谢谢。

推荐答案

对我来说是取消的最好方法所有请求是:

For me best way to cancel all request is:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; //manager should be instance which you are using across application
[manager.session invalidateAndCancel];

这种方法有一个很大的优势:所有正在执行的请求都会调用失败块。我的意思是这个例子:

this approach has one big advantage: all your requests that are executing will call failure block. I mean this one for example:

        [manager GET:url
        parameters:nil
          progress:^(NSProgress * _Nonnull downloadProgress) {
              code
          } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              code
          } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              //this section will be executed
          }];

您需要知道的2件事情:

2 things you need to know:


  • 将取消只与AFHTTPSessionManager的这一确切实例执行请求将用于invalidateAndCancel

  • 后取消,你必须初始化AFHTTPSessionManager的新实例的所有请求 - 当你试图用旧的请求提出异常

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

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