简体   繁体   中英

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()
    }
}

是的,这可以用来避免阻塞主线程的方法之一,但还有很多其他选择,例如使用DispatchGroup链接异步任务, OperationQueue甚至创建自己的队列,在里面做大量的工作并交出UI的东西到主线程,但要记住全局队列只有一个队列,如果你发现它有很多调用,最好创建一个新的帮助器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM