Best/most 从后台线程更新 UI 的常用方法
Best/most common way to update UI from background thread
每次我需要退出关闭以更新 UI 时,我都会向 NSNotficiationCenter
发布通知,但这开始变得有点乏味,让我想知道是否有更好的方法。经过快速搜索后,我发现您可以像这样分派到主队列:
dispatch_async(dispatch_get_main_queue()) {
// update some UI
}
这种方法肯定是代码少,但我想知道它是否提高了可读性。所以我的问题是,Swift 中的 "the way to go" 从后台线程更新 UI 被认为是什么?
这是要走的路:
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// do some background task
dispatch_async(dispatch_get_main_queue()) {
// update some UI
}
}
每次我需要退出关闭以更新 UI 时,我都会向 NSNotficiationCenter
发布通知,但这开始变得有点乏味,让我想知道是否有更好的方法。经过快速搜索后,我发现您可以像这样分派到主队列:
dispatch_async(dispatch_get_main_queue()) {
// update some UI
}
这种方法肯定是代码少,但我想知道它是否提高了可读性。所以我的问题是,Swift 中的 "the way to go" 从后台线程更新 UI 被认为是什么?
这是要走的路:
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// do some background task
dispatch_async(dispatch_get_main_queue()) {
// update some UI
}
}