核心数据并发队列风格MOC getter线程安全 [英] Core Data concurrency queue style MOC getters thread safety

查看:123
本文介绍了核心数据并发队列风格MOC getter线程安全的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑下面的段落直接从NSManagedObjectContext 文档


基于队列的托管对象上下文的Setter方法是线程安全的。
你可以直接在任何线程上调用这些方法。


最大的问题是在ManagedObjectContext上的setters方法,这个上下文拥有的ManagedObjects?或者是在两个?具体来说,如果对于私有队列MOC对象,像这样:

  [privateContext setPersistentStoreCoordinator:self.persistentStoreCoordinator]; 

无论执行此行的线程如何,都是线程安全的,但会是这样:

  [myPrivateQueueOwnedManagedObject setTitle:@My Title]; 

也是线程安全的,文档在这方面真的很模糊,但我的理解是,这不会是线程安全的是正确的吗?



对于ManagedObjectContext中的属性的getter方法,例如询问persistentStoreCoordinator属性,这将是线程安全的。我的理解是它不会是。



一直是我的理解,某些托管对象属性,如objectID是线程安全的,不需要使用performBlock:或performBlockAndWait访问:在托管对象上是否有线程安全的任何其他属性?

解决方案

-setPersistentStoreCoordinator:是线程安全的,因为它是一个setter方法



您可以使用debug标志确认此行为:

> -com.apple.CoreData.ConcurrencyDebug 1



更新




确认文档的第二段,在主线程上访问BOTH MOC和MO是否安全访问主风格队列MOC对象?我的理解是,它是如此的例如setTitle将是确定如果上下文是主风格和线程是主线程。这是因为遗留的原因与线程限制afaik,并且也有很大的帮助,当使用MO为UI更新。


如果上下文定义为主队列,你在主队列(又称为UI线程,也称为主线程),然后是你可以直接访问一切,而不用 -performBlock:。你是上下文所属的线程,所以你正在遵循线程限制规则。


I am really confused by the following paragraph straight from the NSManagedObjectContext documentation:

Setter methods on queue-based managed object contexts are thread-safe. You can invoke these methods directly on any thread.

The big question is setters methods on the ManagedObjectContext but NOT in the ManagedObjects owned by this context? or is it on both?. Specifically if for a private queue MOC object something like this:

[privateContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];

Would be thread safe regardless of the thread executing this line but would something like:

 [myPrivateQueueOwnedManagedObject setTitle:@"My Title];

Also be thread safe?, the documentation is really vague on this, but my understanding is that this would NOT be thread safe is that correct?

What about getters for properties in the ManagedObjectContext such as asking for the persistentStoreCoordinator property would that be thread safe?. My Understanding is that it would not be.

Additionally it has always been my understanding that certain Managed Object properties such as objectID are thread safe and do not need to be accessed using performBlock: or performBlockAndWait: are there any other properties on the Managed Object that are thread safe?

解决方案

-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.

这篇关于核心数据并发队列风格MOC getter线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆