新创建的 NSManagedObject 即使在保存后也会返回临时 objectID

2023-12-23

非常简单的情况。不知道为什么它会引起问题。

我有一个在子 NSManagedObjectContext 中创建新 NSManagedObject 的视图。当用户按下“完成”时,它会保存子上下文,然后保存父上下文,然后使用新创建的对象的 objectID 发布通知。在主视图控制器中,我响应通知并尝试使用以下命令获取新创建的对象existingObjectWithID:error:.

问题是失败,因为 objectID 是临时的(我收到“Cocoa 错误 133000”)。两个上下文的保存是完美的:当我重新加载应用程序时,我可以看到我创建的条目。但当我需要获取新对象的引用时,它失败了。

为什么它给我一个临时对象 IDafter我已经保存了?

注意:我尝试过使用obtainPermanentIDsForObjects:error:,它有效,但是当我尝试使用它来获取对象时,永久 ID 仍然失败。

这是一些代码:

-(IBAction)done:(id)sender
{
    if ([editorDoneNotification isEqualToString:kNOTIFICATION_OBJECTADDED]) {
        // save the temporary moc
        NSError* e;
        if (![self.tempContext save:&e]) { // this is always a successful save
            NSLog(@"Failed to save temporary managed object context: %@", [e localizedDescription]);
            [[[UIAlertView alloc] initWithTitle:@"Database Error"
                                    message:@"Failed to add object."
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
        }
    }
    NSError* e;
    if (![[[AMDataModel sharedDataModel] mainContext] save:&e]) { // this is also successful
        NSLog(@"Failed to save main managed object context: %@", [e localizedDescription]);
        [[[UIAlertView alloc] initWithTitle:@"Database Error"
                                message:@"Failed to edit object."
                               delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil] show];
    }
    else
        [[NSNotificationCenter defaultCenter] postNotificationName:editorDoneNotification object:[self.editingObject objectID]]; 

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

这就是我对通知的回应:

-(void)objectAdded:(NSNotification*)notification
{
    if (self.popoverController && [self.popoverController isPopoverVisible]) {
        [self.popoverController dismissPopoverAnimated:YES];
    }

    NSManagedObjectID* newObjectID = (NSManagedObjectID*)(notification.object);
    NSError* error;
    AMObject* object = (AMObject*)[[[AMDataModel sharedDataModel] mainContext] existingObjectWithID:newObjectID error:&error]; // this is where the cocoa error 133000 happens
    if (error != nil) {
        NSLog(@"ERROR: Could not load new object in main managed object context.");
    }

    GMSMarker* m = [[GMSMarker alloc] init];
    m.position = CLLocationCoordinate2DMake(object.latitudeValue, object.longitudeValue);
    m.userData = object;
    m.map = self.mapView;
    [self.markers addObject:m];
}

-(void)objectEdited:(NSNotification *)notification
{  
    NSManagedObjectID* editedObjectID = (NSManagedObjectID*)notification.object;
    NSError* error = nil;
    AMObject* object = (AMObject*)[[[AMDataModel sharedDataModel] mainContext] existingObjectWithID:editedObjectID error:&error];
    if (error != nil) {
        NSLog(@"Error could not load edited object in main managed object context");
    }

    //update the UI based on edit

    if ([self.popoverController isPopoverVisible]) {
        [self.popoverController dismissPopoverAnimated:YES];
        self.popoverController = nil;
    }
}

因为子级不会从父级 MOC 处获取更新。父 MOC 将更新其自己的实例NSManagedObject具有永久 ID,但该更改不会被推送到该实例NSManagedObject属于子 MOC。

Update 1

我不使用-objectID在这个情况下。它有一些用途,但它不是永久的唯一 ID。在这种情况下,我更喜欢将自己的 uniqueID 添加到实体中,然后从主上下文中获取它。

您也可以只监听上下文保存通知或使用将接收更新的 NSFetchedResultsController 。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

新创建的 NSManagedObject 即使在保存后也会返回临时 objectID 的相关文章

随机推荐