Michael Hsu
Michael Hsu

Reputation: 982

ios/swift dispatch queues - global and main queue concepts

So if I were to do something heavy, and then update the UI, would this be the best way of doing it?

DispatchQueue.global().async {
    // Do something heavy here, such as adding 10000 objects to an array

    DispatchQueue.main.async {
        // Update UI here after the heavy lifting is finished, such as tableView.reloadData()
    }
}

Upvotes: 3

Views: 795

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Yes, this one of the ways you can use to avoid blocking the main thread , but there is many other alternatives such as using DispatchGroup for chaining asynchronous tasks ,OperationQueue or even create your own queue and inside it do heavy work and hand the UI things to main thread , but to keep in mind the global queue is only one queue if you find that there is a lot of calls to it , it's better to create a new helper one

Upvotes: 4

Related Questions