dispatch_async在NSManagedObjectContext的performBlock [英] dispatch_async in NSManagedObjectContext's performBlock
问题描述
根据WWDC 2012视频核心数据最佳实践, dispatch_sync
应用于在 performBlock
上下文,它是作为
NSPrivateQueueConcurrencyType
类型创建的。
为什么? / p>
我可以使用 dispatch_async(dispatch_get_main_queue(),0)
...私下调用一些UI相关的回调队列的上下文 performBlock
?
NSPrivateQueueConcurrencyType
管理它自己的内部队列,不喜欢你试图离开它的一个线程来做你想做的事情(事实上,我相信它抛出异常,当发生类型的行为)。有几种方法来处理这种情况,当然信号信号是更可接受的设计模式,以获得真正的异步感觉,但正如你所说, dispatch_sync
通常是
According to the WWDC 2012 video, "Core Data Best Practices", dispatch_sync
should be used to run some kind of callback in performBlock
of context, which is created as a type of NSPrivateQueueConcurrencyType
.
Why is that?
Can I use dispatch_async(dispatch_get_main_queue(), 0)
... to call some UI-related callbacks in private queue's context's performBlock
?
No. NSPrivateQueueConcurrencyType
manages it's own internal queue, and does not enjoy you trying to leave one of it's threads to do what you want to do (in fact, I believe it throws exceptions when this type of behavior occurs). There's a few ways of handling this, of course signalling semaphores being the more acceptable design pattern to get the truly "async feel", but as you note, dispatch_sync
is usually the way to go.
这篇关于dispatch_async在NSManagedObjectContext的performBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!