Reputation: 28
I'm planning on using a web service call from Xcode and need it to be performant. Do I call the service from a background thread and then update the UI on another thread?
Upvotes: 0
Views: 45
Reputation: 359
I think this should work for your purposes (written in Swift):
// Go to an asynchronous thread
// .userInitiated quality of service for a high-priority task
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
// Make sure you have a reference to self if you need it here
guard let self = self else {
return
}
// Make your asynchronous web service call here
// Now return to the main thread
DispatchQueue.main.async { [weak self] in
// Make sure you have a reference to self if you need it here
guard let self = self else {
return
}
// Update your UI here
}
}
For more on DispatchQueue
and the different qualities of service, I recommend this tutorial.
Upvotes: 1