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

标签 ios objective-c core-data

非常简单的情况。不确定为什么会导致问题。

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

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

为什么它在我已经保存它之后 给我一个临时对象 ID?

注意:我试过使用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;
    }
}

最佳答案

因为 child 没有从父 MOC 得到更新。父 MOC 将使用永久 ID 更新其自己的 NSManagedObject 实例,但该更改不会下推到属于子 MOC 的 NSManagedObject 实例。

更新1

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

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

关于ios - 新创建的 NSManagedObject 即使在保存后也会返回临时 objectID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18410456/

相关文章:

iphone - Objective-C : Adaptive Toolbar

ios - 在 Objective C 中动态设置 C 结构数组的大小?

ios - 在 UiWebView 中 - NSURLConnection/CFURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-108)

ios - 在 UITableViewController 之外使用 NSFetchedResultsController

ios - 我怎么知道在 iOS 中是否发送了推送通知?

ios - 解析推送通知和 iOS 8.2 的警报标题

ios - UICollectionView 水平单元格

iPhone - Objective C - UITableView 内的 UIScrollView 渲染有趣

ios - RestKit崩溃— RKManagedObjectRequestOperation.m第873行

objective-c - 尝试使用 CONTAINS 时 NSPredicate 崩溃