加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – NSManagedObjectContext混淆

发布时间:2020-12-16 04:56:43 所属栏目:百科 来源:网络整理
导读:我正在学习CoreData.显然,你所包含的主要类之一是NSManagedObjectContext.我不清楚这个的确切作用.从我读过的文章中,您似乎可以拥有多个NSManagedObjectContexts.这是否意味着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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读