Home > front end >  Do I need place urlsession datatask on another DispatchQueue or it's done automatically?
Do I need place urlsession datatask on another DispatchQueue or it's done automatically?

Time:12-19

Didn't have any experience with datatasks and urlsessions before. So I'm curious. Case is - trying to have a table with infinite scroll. When this method called

tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath])

from UITableViewDataSourcePrefetching and after check that at least one indexpath there has row bigger then current max items - I'm starting to load new data with urlsession datatask. And question is - do I need to do something like

let queue = DispatchQueue.gloabl(qos: .userInteractive)
queue.async {
  myTaskGoesHere
}

CodePudding user response:

URLSession data tasks are always running on a background thread. Therefore, you have to switch to the main queue in the completion handler closure to update the user interface. Because the answer of the data task is delivered on the same thread on which iOS started the task.

By the way, .userInteractive also runs on the main thread. From the documentation:

User-interactive tasks have the highest priority on the system. Use this class for tasks or queues that interact with the user or actively update your app's user interface. For example, use this for class for animations or for tracking events interactively.

  • Related