Core Data concurrency queue style MOC getters thread safety

霸气de小男生 提交于 2019-12-03 13:29:06

-setPersistentStoreCoordinator: is thread safe as it is a setter method on the managed object context.

-setTitle: is not because you are calling a setter on a managed object.

You can confirm this behavior by using the debug flag:

-com.apple.CoreData.ConcurrencyDebug 1

Which will throw an assertion when you violate thread confinement.

Update

While we are at it just to confirm the second paragraph on the documentation, is it safe to access BOTH MOC and MO for main style queue MOC objects while on the main thread?. my understanding is that it is so for instance setTitle would be ok if the context was Main style and the thread was the main thread. This is for legacy reasons with thread confinement afaik, and also a great help when using the MO for UI updates.

If the context as defined as main queue and you are on the main queue (aka UI thread, aka main thread) then yes you can access everything directly without a -performBlock:. You are on the thread that the context belongs to so you are following the thread confinement rules.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!