0

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:

  1. Why does the second data task appear not to run until the first one completes?
  2. Why does cancel() work but suspend() does not?

Sorry if these are dumb questions, I'm just starting with UIKit and Swift.

Stuart
  • 1
  • "I am instantiating a URLSessionDataTask" Where? – El Tomato Oct 12 '21 at 02:59
  • This is resolved now. Thanks to [link](https://dev.to/shawonashraf/loading-images-from-urls-in-swift-without-urlsession-127g). Turns out I had a tight loop inside my asynchronous call to the server that was downloading the images using a synchronous mechanism (the "easy way" as described in the link). Once I switched to an asynchronous mechanism, everything was .resolved – Stuart Oct 12 '21 at 22:23

0 Answers0