Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading