核心数据:子上下文是否会为新插入的对象获得永久的对象 ID? [英] Core Data: Do child contexts ever get permanent objectIDs for newly inserted objects?

查看:17
本文介绍了核心数据:子上下文是否会为新插入的对象获得永久的对象 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个托管对象上下文设置的应用程序,如下所示:

  • 父上下文:NSPrivateQueueConcurrencyType,链接到持久存储.
  • 主上下文:NSMainQueueConcurrencyType,父上下文的子级.

当向主上下文插入一个新的托管对象时,我先保存主上下文,然后像这样保存父上下文:

[context performBlockAndWait:^{NSError * 错误 = 零;if (![上下文保存:&error]) {NSLog(@"Core Data 保存错误 %@, %@", error, [error userInfo]);}}];[parentContext performBlock:^{NSError *error = nil;BOOL 结果 = [parentContext save: &error];如果(!结果){NSLog( @"Core Data 在父上下文中保存错误 %@, %@", error, [error userInfo] );}}];

我的理解是当管理对象第一次创建时,它有一个临时的objectID.然后主上下文被保存,这个对象及其临时 ID 到达父上下文.然后保存父上下文.当保存最后一个上下文时,父上下文中的临时 objectID 被转换为永久的 objectID.

所以:

  • 永久对象 ID 是否会自动传播回来到主要(子)上下文?
  • 当我强制获取对象永久 ID 时[NSManagedObjectContext获取PermanentIDsForObjects:error:],然后后台应用程序,重新激活它,重新加载,使用主上下文的objectWithID:获取对象,并访问一个属性,我得到<块引用>

    CoreData 无法满足...的故障".

这种方法有什么问题?

解决方案

这是一个已知的错误,希望很快修复,但一般来说,获得一个永久 ID 就足够了,前提是你在第一次保存数据之前这样做孩子,你只包括插入的对象:

[moc gainPermanentIDsForObjects:moc.insertedObjects.allObjects 错误:&error]

在一些复杂的情况下,最好在创建实例后立即获取永久 ID,尤其是在关系复杂的情况下.

您如何以及何时调用 obtainPermanentIDsForObjects?

我不关注有关应用程序崩溃的部分.也许更好的解释会有所帮助.

I have an app with two managed object contexts setup like this:

  • Parent Context: NSPrivateQueueConcurrencyType, linked to the persistent store.
  • Main Context: NSMainQueueConcurrencyType, child of Parent Context.

When insert a new managed object to the main context, I save the main context and then the parent context like this:

[context performBlockAndWait:^{
    NSError * error = nil;
    if (![context save: &error]) {
        NSLog(@"Core Data save error %@, %@", error, [error userInfo]);
    }
}];

[parentContext performBlock:^{
    NSError *error = nil;
    BOOL result = [parentContext save: &error];
    if ( ! result ) {
        NSLog( @"Core Data save error in parent context %@, %@", error, [error userInfo] );
    }
}];

My understanding is that when the manage object is first created, it has a temporary objectID. Then the main context is saved and this object, with its temporary ID, gets to the parent context. Then the parent context is saved. When this last context is saved, the temporary objectID in the parent context gets transformed into a permanent objectID.

So:

  • Does the permanent object ID ever get propagated automatically back to the main (child) context?
  • When I force to get the object permanent ID with [NSManagedObjectContext obtainPermanentIDsForObjects:error:], then background the app, reactivate it, reload, get the object using main context's objectWithID:, and access a property, I get

    "CoreData could not fulfill a fault for ...".

What is wrong with this approach?

解决方案

It is a known bug, hopefully fixed soon, but in general, obtaining a permanent ID is sufficient, provided you do so before you save the data in the first child, and you only include the inserted objects:

[moc obtainPermanentIDsForObjects:moc.insertedObjects.allObjects error:&error]

In some complex cases, it is better to get a permanent ID as soon as you create the instance, especially if you have complex relationships.

How and when are you calling obtainPermanentIDsForObjects?

I do not follow the part about the app crashing. Maybe a better explanation would help.

这篇关于核心数据:子上下文是否会为新插入的对象获得永久的对象 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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