objective-c – NSManagedObjectContext混淆

前端之家收集整理的这篇文章主要介绍了objective-c – NSManagedObjectContext混淆前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在学习CoreData.显然,你所包含的主要类之一是NSManagedObjectContext.我不清楚这个的确切作用.从我读过的文章中,您似乎可以拥有多个NSManagedObjectContexts.这是否意味着NSManagedObjectContext基本上是后端的副本?

当存在多个不同的副本时,这将如何解决为一致的后端?

那么,基本上有两个问题:

NSManagedContext是后端数据库的副本吗?

和…

例如,假设我在上下文A中进行了更改,并在上下文B中进行了一些其他更改.然后我先调用保存在A上,然后是B? B会占上风吗?

谢谢

解决方法

NSManagedObjectContext不是后端数据库的副本. documentation将其描述为便笺簿

An instance of NSManagedObjectContext represents a single “object
space” or scratch pad in an application. Its primary responsibility is
to manage a collection of managed objects. These objects form a group
of related model objects that represent an internally consistent view
of one or more persistent stores. A single managed object instance
exists in one and only one context,but multiple copies of an object
can exist in different contexts. Thus object uniquing is scoped to a
particular context.

NSManagedObjectContext只是以事务方式更改托管对象的临时位置.当您对上下文中的对象进行更改时,它不会影响后端数据库,直到您保存上下文,并且如您所知,您可以拥有多个上下文,您可以对其进行更改,这对于concurrency非常重要.

对于问题2,谁占优势的答案将取决于您为上下文设置的merge policy以及最后一个将被称为B的那个.以下是可以设置的合并策略,这将影响要保存的第二个上下文.

NSErrorMergePolicyType
Specifies a policy that causes a save to fail
if there are any merge conflicts.

NSMergeByPropertyStoreTrumpMergePolicyType
Specifies a policy that
merges conflicts between the persistent store’s version of the object
and the current in-memory version,giving priority to external
changes.

NSMergeByPropertyObjectTrumpMergePolicyType
Specifies a policy that merges conflicts between the persistent store’s version
of the object and the current in-memory version,giving priority to
in-memory changes.

NSOverwriteMergePolicyType
Specifies a policy that
overwrites state in the persistent store for the changed objects in
conflict.

NSRollbackMergePolicyType Specifies a policy that discards in-memory state changes for objects in conflict.

原文链接:https://www.f2er.com/c/120197.html

猜你在找的C&C++相关文章