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

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

问题描述

我有一个应用程序与两个管理对象上下文设置如下:




  • 父上下文:NSPrivateQueueConcurrencyType,链接到持久存储。 / li>
  • 主上下文:NSMainQueueConcurrencyType,父上下文的子项。



主要上下文,我保存主上下文,然后父节点上下文如下:

  [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(@父上下文中的核心数据保存错误%@,%@,错误,[错误userInfo]);
}
}];

我的理解是,当管理对象首次创建时,它有一个临时 objectID 。然后保存主上下文,并且该对象及其临时ID获取父上下文。然后保存父上下文。保存最后一个上下文时,父上下文中的临时 objectID 将转换为永久性 objectID



所以:




  • 永久物件ID自动返回
    到主(子)上下文?

  • 当我强制获取对象永久ID与
    [NSManagedObjectContext getsPermanentIDsForObjects:error :] ,然后背景应用程序,重新激活它,重新加载,使用主上下文的 objectWithID:获取对象,并访问属性, / p>


    CoreData无法解决...的错误。



此方法有什么问题?



解决方案

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

  [moc obtainPermanentIDsForObjects:moc.insertedObjects.allObjects error:& error] 
/ pre>

在某些复杂的情况下,最好在创建实例时获取永久ID,尤其是当您有复杂的关系时。



如何以及何时呼叫 getsPermanentIDsForObjects



关于应用程序崩溃的部分。也许更好的解释会有所帮助。


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.

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

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