ios 在主线程上发布 NSNotification
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15813764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Posting NSNotification on the main thread
提问by RunLoop
I found the following code snippet which allows NSNotification
to be posted on the main thread from any background thread. I would like to know if this is a safe and acceptable practice please?
我发现以下代码片段允许NSNotification
从任何后台线程发布到主线程上。我想知道这是否是一种安全且可接受的做法?
dispatch_async(dispatch_get_main_queue(),^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"ImageRetrieved"
object:nil
userInfo:imageDict];
});
采纳答案by Anoop Vaidya
Yes you can.
是的,你可以。
Generally you want the NSNotifications to be sent on the main , especially if they trigger UI activities like dismissing a modal login dialog.
通常,您希望 NSNotifications 在 main 上发送,尤其是当它们触发 UI 活动时,例如关闭模式登录对话框。
Delivering Notifications To Particular Threads
Regular notification centers deliver notifications on the thread in which the notification was posted. Distributed notification centers deliver notifications on the main thread. At times, you may require notifications to be delivered on a particular thread that is determined by you instead of the notification center. For example, if an object running in a background thread is listening for notifications from the user interface, such as a window closing, you would like to receive the notifications in the background thread instead of the main thread. In these cases, you must capture the notifications as they are delivered on the default thread and redirect them to the appropriate thread.
常规通知中心在发布通知的线程上发送通知。分布式通知中心在主线程上传递通知。有时,您可能需要在由您而不是通知中心确定的特定线程上传递通知。例如,如果在后台线程中运行的对象正在侦听来自用户界面的通知,例如窗口关闭,您希望在后台线程而不是主线程中接收通知。在这些情况下,您必须捕获在默认线程上传递的通知并将它们重定向到适当的线程。
回答by Undo
Yes
是的
This is - you are getting into the main thread and posting your notification. Can't get any safer than that.
这是 - 您正在进入主线程并发布您的通知。没有比这更安全的了。
回答by WhipsterCZ
YES
是的
Swift 2syntax
Swift 2语法
dispatch_async(dispatch_get_main_queue()) {
NSNotificationCenter.defaultCenter().postNotificationName("updateSpinner", object: nil, userInfo: ["percent":15])
}
Swift 3syntax
Swift 3语法
DispatchQueue.main.async {
NotificationCenter.default.post(name: "updateSpinner", object: nil, userInfo: ["percent":15])
}
回答by Dan Rosenstark
Somewhere along the line this became possible with:
沿着这条线的某个地方,这成为可能:
addObserver(forName:object:queue:using:)
which is here, but the whole point is the queue
object.
这是here,但重点是queue
对象。
The operation queue to which block should be added. If you pass
nil
, the block is run synchronously on the posting thread.
应添加块的操作队列。如果通过
nil
,则块在发布线程上同步运行。
So how do you get the queue that corresponds to the main runloop?
那么如何得到主runloop对应的队列呢?
let mainQueue = OperationQueue.main
let mainQueue = OperationQueue.main
Note: this is when you are subscribing to notifications, so you do it once and you're done. Doing it on every single call is terribly redundant.
注意:这是您订阅通知的时间,因此您只需执行一次即可完成。在每次调用时都这样做是非常多余的。