NSURLSessionTask. Pause does not work

Here's what Apple's documentation says about the suspend method of the NSURLSessionTask class

The task, suspended, does not create network traffic and is not subject to timeouts.

Ok So I run 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") }) 

The delay function is just a wrapper around dispatch_after , and a request to http://httpbin.org/delay/10 gives an answer in 10 seconds. Waiting for a response, I pause the task. However, this does not work. After 60 seconds, the termination block is called with a timeout error. Can someone explain what happened?

+5
source share
2 answers

This seems like normal behavior, but Apple’s clearer documentation will be helpful in figuring out what we see.

The apple documentation does not provide a detailed explanation of how the suspension works, or when it should be used. However, my view (based on testing and research) is that suspend() should only be used to load a job. Data tasks should only use resume() (to run the task) and cancel() when necessary.

My testing using Xcode and Charles Proxy showed that a suspended data task does not affect network traffic, as indicated in the Apple documentation. In other words, network traffic is created.

I observed two things using pause and data tasks:

1) If it is called immediately after resuming, it does not affect the data task. It does not suspend network traffic and does not prohibit network or server problems; a successful response was received in the callback.

2) If he called the dispatch.asyncAfter callback, he still does not suspend network traffic, however, the callback receives a "timeout request" error instead of a successful response. According to Charles Proxy, the request was successful. It is this result that makes me think that suspend() should not be used with data tasks. The result of this callback is practically useless, in my opinion.

Cancel a task with data:

cancel() works as expected. The client (you) closes the connection before receiving a complete response from the server. This can be done immediately after calling resume() or later (before the request completes, of course).

+5
source

I accept that suspend () does not affect URLSessionDataTask

  let dataTask = URLSession.shared.dataTask(with: request) { (data, urlResponse, error) in print("response is received all the time") } } dataTask.resume() dataTask.suspend() 

However, when I try to cancel it,

 let dataTask = URLSession.shared.dataTask(with: request) { (data, urlResponse, error) in print("response is cancelled") } } dataTask.resume() dataTask.cancel() 
0
source

Source: https://habr.com/ru/post/1259927/


All Articles