I am instantiating a URLSessionDataTask
that downloads hundreds of thumbnail images and stuffs them into an array. Each entry in the array is used to populate a cell in a UITableView
instance. It works exactly as expected.
However, I want to give the user the opportunity to click on a cell and initiate a second instance of a URLSessionDataTask
in an effort to download additional details associated with that thumbnail. And I don't want the user to wait until the first data task finishes.
That's where the problem lies. The second URLSessionDataTask
instance doesn't retrieve the data I need until the first URLSessionDataTask
instance completes. I guess I don't understand this since my understanding is that the first task is an asynchronous background task.
So I tried a workaround such that when the user clicks on a cell to grab the detail information I would suspend the first task, download the detail info and then resume the first task.
I want to do something roughly like this:
var firstTask = callThumbnailServer(queryString: createHTTPQueryString())
func userAsksForDetails() {
firstTask.suspend()
var secondTask = callDetailServer(queryString: createHTTPQueryString())
firstTask.resume()
}
But firstTask.suspend()
appears to have no effect. Now, firstTask.cancel()
does successfully cancel the first task, but I don't want to cancel, I want to suspend/resume.
So I guess I have two questions:
- Why does the second data task appear not to run until the first one completes?
- Why does cancel() work but suspend() does not?
Sorry if these are dumb questions, I'm just starting with UIKit and Swift.