NSURLSessionTask.暂停不起作用 [英] NSURLSessionTask. Suspend does not work

查看:382
本文介绍了NSURLSessionTask.暂停不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Apple文档关于NSURLSessionTask类的suspend方法的内容

This is what Apple's documentation says regarding suspend method of NSURLSessionTask class

任务暂停后不会产生网络流量,也不会超时.

A task, while suspended, produces no network traffic and is not subject to timeouts.

好的.因此,我正在运行以下简单代码:

Ok. So I'm running the following simple code:

        let url   = NSURL(string: "http://httpbin.org/delay/10")!
        let urlRequest = NSURLRequest(URL: url)

        self.task = NSURLSession.sharedSession().dataTaskWithURL(urlRequest.URL!, completionHandler: {

            data, response, error in print("completion ERROR \(error)")
        })

        self.task.resume()

        print("Start")
        delay(5, closure: {

            self.task.suspend()

            print("Suspend")
        })

函数delay只是dispatch_after的包装,对http://httpbin.org/delay/10的请求在10秒后给出响应.在等待响应的过程中,我暂停了任务.但是,这不起作用.在60秒内,完成块被调用,并带有超时错误.谁能解释一下怎么了?

Function delay is simply a wrapper around dispatch_after and a request to http://httpbin.org/delay/10 gives response after 10 seconds. In the middle of waiting for response I suspend the task. However that does not work. In 60 seconds the completion block is called with timeout error. Can anybody please explain what's wrong?

推荐答案

这似乎是正常现象,但是Apple提供的更多权威性文档对于阐明我们所看到的内容将很有用.

Apple的文档未提供有关暂停如何工作或何时使用的详细说明.但是,我的观点(基于测试和研究)是suspend()仅应用于下载 任务.数据任务仅应在适当时使用resume()(启动任务)和cancel().

Apple’s documentation does not provide a detailed explanation of how suspend works, or when it should be used. However, my view (based on testing and research) is that suspend() should only be used for download tasks. Data tasks should only use resume() (to start the task) and cancel() when appropriate.

我使用Xcode和Charles Proxy进行的测试显示,暂停的数据任务对网络流量没有影响,如Apple文档中所述.换句话说,产生了网络流量.

My testing, using Xcode and Charles Proxy, revealed a suspended data task has no effect on the network traffic as indicated in Apple’s documentation. In other words, network traffic is produced.

我观察到了使用挂起和数据任务的两件事:

1)如果在恢复后立即调用它,则对数据任务没有任何影响.它不会暂停网络流量,也不会阻止任何网络或服务器端问题,在回调中会收到成功的响应.

1) If it's called right after resume, it has no effect on the data task. It does not suspend network traffic and barring no network or server side-side issues, a successful response is received in the callback.

2)如果在dispatch.asyncAfter回调中调用它,它仍然不会挂起网络流量,但是该回调会收到请求超时"错误,而不是成功的响应.根据查尔斯·普林斯(Charles Proxy)的说法,请求是成功的.正是这个结果使我相信suspend()不应与数据任务一起使用.在我看来,此回调的结果实际上是没有用的.

2) If it's called in the dispatch.asyncAfter callback, it still does not suspend the network traffic, however the callback gets a "request timeout" error instead of a successful response. According to Charles Proxy, the request is successful though. It is this result that leads me to believe that suspend() should not be be used with data tasks. The result of this callback is essentially useless in my opinion.

取消数据任务:

cancel()可以正常工作.客户端(您)在从服务器获得完整响应之前先关闭连接.可以在调用resume()之后或稍后(当然,在请求完成之前)完成此操作.

cancel() works as expected. The client (you) closes the connection before getting a complete response from the server. This can be done right after calling resume() or at a later time (before the request has completed of course).

这篇关于NSURLSessionTask.暂停不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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