简体   繁体   English

在这种情况下,dispatch_async(dispatch_get_main_queue(),...)是否必要?

[英]Is dispatch_async(dispatch_get_main_queue(), …) necessary in this case?

I came across this piece of code, and I can't quite figure out why the author did this. 我遇到了这段代码,我无法弄清楚作者为什么会这样做。 Take a look at this code: 看看这段代码:

someMethodStandardMethodUsingABlock:^() {
    dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:"notif" object:nil];
    });
}];

I have a method with a completion block, and in this block a notification has to be posted. 我有一个带有完成块的方法,在这个块中必须发布通知。 I don't quite understand why the dispatch_async on the main queue is necessary in this case. 我不太明白为什么在这种情况下主队列上的dispatch_async是必要的。 The block will already be run on the main thread, and even if it wasn't I don't think it would really matter would it? 该块已经在主线程上运行,即使不是,我也不认为它会真的重要吗? I would simply have written this: 我只想写下这个:

someMethodStandardMethodUsingABlock:^() {
    [[NSNotificationCenter defaultCenter] postNotificationName:"notif" object:nil];
}];

And it does work in my testing. 它确实在我的测试中起作用。

If you can help me shed some light on this, I'd really appreciate it! 如果你能帮助我阐明一下,我真的很感激!

Matt 马特

These 2 sentences from the NSNotificationCenter Class Reference suggest a couple of possible reasons: NSNotificationCenter类参考中的这两句话表明了几个可能的原因:

A notification center delivers notifications to observers synchronously. 通知中心同步向观察者发送通知。 In other words, the postNotification: methods do not return until all observers have received and processed the notification. 换句话说,postNotification:方法在所有观察者都收到并处理通知之前不会返回。

... ...

In a multithreaded application, notifications are always delivered in the thread in which the notification was posted, which may not be the same thread in which an observer registered itself. 在多线程应用程序中,通知始终在发布通知的线程中传递,这可能与观察者注册自己的线程不同。

So perhaps (a) the author doesn't want the code to block until all observers have processed the notification, and/or (b) he wants to ensure that the observer methods run on the main thread. 因此,或许(a)作者不希望代码在所有观察者处理通知之前阻塞,和/或(b)他想确保观察者方法在主线程上运行。

Sometimes you need to run methods that fire some execution asynchronously and return right away. 有时您需要运行异步触发某些执行的方法并立即返回。 Eg some of the AppDelegate 'key' methods like applicationDidBecomeActive , or applicationDidEnterBackground , need to be executed and return quickly so the OS doesn't kill your app. 例如,某些AppDelegate '关键'方法(如applicationDidBecomeActiveapplicationDidEnterBackground )需要执行并快速返回,因此操作系统不会终止您的应用程序。

I don't know if that is the case of your question, but it is a possible explanation of the usage of dispatch_async . 我不知道你的问题是否属于这种情况,但这可能是对dispatch_async使用的一种解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 dispatch_async队列(dispatch_get_main_queue(),^ {})内的weakself - weakself inside dispatch_async queue(dispatch_get_main_queue(), ^{}) 为什么从不调用dispatch_async(dispatch_get_main_queue()之后的块? - Why does the block after dispatch_async(dispatch_get_main_queue() never get called? 从xctest执行dispatch_async(dispatch_get_main_queue() - Execute dispatch_async(dispatch_get_main_queue() from xctest 使用dispatch_async + dispatch_get_main_queue进行UI更新有什么意义? - What's the point in using dispatch_async + dispatch_get_main_queue for UI updates? dispatch_async(dispatch_get_main_queue(),^ {...});等到完成? - Does dispatch_async(dispatch_get_main_queue(), ^{…}); wait until done? dispatch_async(dispatch_get_main_queue(),^ {}); 从iOS7背景线程慢 - dispatch_async(dispatch_get_main_queue(), ^{}); from Background Thread slow on iOS7 了解ios UIView动画块和dispatch_async(dispatch_get_main_queue)块 - Understanding ios UIView animation block and dispatch_async(dispatch_get_main_queue) block dispatch_async(dispatch_get_main_queue()不适用于iPad,但适用于iPhone - dispatch_async(dispatch_get_main_queue() is not working on iPad, but working on iPhone 主线程中的di​​spatch_get_main_queue() - dispatch_get_main_queue() in main thread dispatch_get_main_queue()的线程ID - thread id of dispatch_get_main_queue()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM