existingObjectWithID:error:无法返回父上下文的对象

时间:2019-05-12 15:04:21

标签: macos core-data

我的行为existingObjectWithID:error:

我有一个带有父上下文的上下文。当我知道(并已记录,请参见下文)父级上下文中存在所需的对象时,在子级上下文中“不时地”调用existingObjectWithID:error:会返回错误133000。

当我说“不时”时,在最后创建的NSManagedObject上永远不会发生,但在所有其他NSNS上却总是发生(我在子上下文中创建它们并将它们保存到父上下文中)。

我希望existingObjectWithID:error:总是从父存储中获取对象。怎么了?

一些代码说明了我说的话:

- (NSManagedObject*)    backgroundObjectFromMainObject:(NSManagedObject*)object
{
    NSError             *error  = nil ;
    NSManagedObjectID   *oid    = [object objectID] ;
    NSManagedObject     *crd    = [self.context existingObjectWithID:oid
                                                   error:&error] ;
    [error logDetailsOfError] ;

    if (error)
    {
        NSLog(@"Registered in master : %@",[self.context.parentContext registeredObjects]) ;
    }

    return crd ;
}

获取以下日志:

2019-05-12 16:52:43.607563+0200 MyOwnNotes[75836:10752916] Error domain: NSCocoaErrorDomain code: 133000
2019-05-12 16:52:43.609641+0200 MyOwnNotes[75836:10752916]    Tentative d’accès à un objet introuvable en magasin.    <= THAT MEANS "Try to access unstored object"
2019-05-12 16:52:43.609737+0200 MyOwnNotes[75836:10752916]   objectID => 0x600000250380 <x-coredata:///NotesHierarchyElement/t60559E22-AFA4-4E1A-8832-E5CF620A9D244>
2019-05-12 16:52:43.609953+0200 MyOwnNotes[75836:10752916] Registered in master : {(
    <NotesHierarchyElement: 0x600002118f00> (entity: NotesHierarchyElement; id: 0x600000250380 <x-coredata:///NotesHierarchyElement/t60559E22-AFA4-4E1A-8832-E5CF620A9D244> ; data: {
    father = nil;
    sons =     (
    );
    title = "16:52:35";
    url = nil;
}),
    <NotesHierarchyElement: 0x6000021043c0> (entity: NotesHierarchyElement; id: 0x6000002502e0 <x-coredata:///NotesHierarchyElement/t60559E22-AFA4-4E1A-8832-E5CF620A9D246> ; data: {
    father = nil;
    sons =     (
    );
    title = "16:52:41";
    url = nil;
})
)}

通过注册对象的日志,您可以清楚地看到具有objectId x-coredata:///NotesHierarchyElement/t60559E22-AFA4-4E1A-8832-E5CF620A9D244的对象存在于父存储中(这是第二个对象(最旧的,我可以毫无问题地检索到最新的对象))。

0 个答案:

没有答案