Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 新创建的NSManagedObject即使在保存后也会返回临时objectID_Ios_Objective C_Core Data - Fatal编程技术网

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

Ios 新创建的NSManagedObject即使在保存后也会返回临时objectID,ios,objective-c,core-data,Ios,Objective C,Core Data,非常简单的情况。不知道为什么会引起问题 我有一个视图可以在子NSManagedObject上下文中创建新的NSManagedObject。当用户按“完成”时,它保存子上下文,然后保存父上下文,然后用新创建的对象的objectID发布通知。在主视图控制器中,我响应通知并尝试使用existingObjectWithID:error:获取新创建的对象 问题是这失败了,因为objectID是临时的(我得到一个“Cocoa error 133000”)。对这两个上下文的保存是完美的:当我重新加载应用程序时

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

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

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

为什么在我保存了一个临时对象ID之后,它会给我一个临时对象ID

注意:我已尝试使用
获取永久性对象:错误:
,这是有效的,但当我尝试使用永久性ID获取对象时,永久性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将使用永久ID更新自己的
NSManagedObject
实例,但该更改不会下推到属于子MOC的
NSManagedObject
实例

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


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

您是否尝试过获取对象的永久ID?我通过调用

NSError *error = nil;
[self.context obtainPermanentIDsForObjects:@[object1, object2]
                                     error:&error];

然后是我的保存方法。我构建上下文的方式是,有一个I/O上下文,它是我的“读取上下文”(用于我的主线程活动的上下文)的父级,它是我的后台任务上下文的父级。在这种情况下,“保存”会一直向上传播。

因此,在这种情况下,在父MOC中获取对新创建对象的引用的最佳方法是什么?您使用什么标识符?我创建自己的全局唯一ID并将其存储在对象中。将
nil
传递到error是一件坏事。它隐藏了让你挠头的错误。总是,总是,总是检查错误。100%同意马库斯的观点-这是一个来自内存的快速片段。我已经编辑了完整性。是的。我确实尝试过获得永久身份证(在我的问题中提到)。但是主上下文仍然无法获取对象。哦。。。我想我意识到了我的错误:我在保存上下文后调用了对象的
obtainPermanentIDsForObjects:error:
。我也会试试另一种方法。@Marcuszarra:
ActainPermanentidsforObjects
就是这样做的。