托管对象上下文的临时对象

时间:2016-04-05 11:27:14

标签: objective-c core-data

我尝试创建和更新临时NSManagedObject,然后将其插入托管对象上下文进行保存,但在保存状态时,我收到错误Error = Error Domain=NSCocoaErrorDomain Code=1550

更多详情:

我有下一个模型:(“联系”对象模型)

Contact Object model

“ContactNumber”对象模型:

ContactNumber" Object model

他们的关系:

relationships

我有一个带有属性的自定义NSView类:

@property (nonatomic, strong) Contact *selectedContact;

在本课程中,我有一个“添加联系人”按钮。当我按下它时,我创建了一个新的临时对象“联系人”:

-(void)createNewContact
{
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact"
                                              inManagedObjectContext:[MTAppDelegate managedObjectContext]];
    Contact *cnt = [[Contact alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
    cnt.isFavorite = @0;
    cnt.isGroupHeader = @0;
    cnt.defaultNumber = @0;
    [self setSelectedContact:cnt];
    [self createPhoneNumber];
} 

并在此对象中添加新的“联系号码”:

-(void)createPhoneNumber
{
    NSEntityDescription *number = [NSEntityDescription entityForName:@"ContactNumber" inManagedObjectContext:[MTAppDelegate managedObjectContext]];
    ContactNumber *newNumber = [[ContactNumber alloc] initWithEntity:number insertIntoManagedObjectContext:nil];
    [newNumber setValue:@"" forKey:@"number"];
    [newNumber setValue:@"-" forKey:@"speedDial"];
    [newNumber setValue:@"Other" forKey:@"type"];
    [newNumber setValue:_selectedContact forKey:@"contact"];

    [_selectedContact addContactNumbersObject:newNumber];
}

使用_selectedContact对象进行一些操作后,我需要将其保存在我的MOC中。我接下来做了:

NSError *error = nil;
        [[MTAppDelegate managedObjectContext] insertObject:_selectedContact];
        if (![[MTAppDelegate managedObjectContext] save:&error])
        {
            NSLog(@"Error = %@", error);
        }

我收到了下一个错误:

Error = Error Domain=NSCocoaErrorDomain Code=1550 "contactNumbers is not valid." UserInfo={Dangling reference to an invalid object.=null, NSValidationErrorValue=Relationship 'contactNumbers' on managed object (0x6080000d2210) <Contact: 0x6080000d2210> (entity: Contact; id: 0x6080000369c0 <x-coredata:///Contact/t3638ED71-98B8-408A-B640-D25063C79E762> ; data: {
    company = 44;
    contactNumbers =     (
        "0x608000037920 <x-coredata:///ContactNumber/t3638ED71-98B8-408A-B640-D25063C79E763>"
    );
    defaultNumber = 0;
    firstName = 11;
    isFavorite = 0;
    isGroupHeader = 0;
    lastName = 33;
    middleInitial = 22;
}) with objects {(
    <ContactNumber: 0x6080000abb20> (entity: ContactNumber; id: 0x608000037920 <x-coredata:///ContactNumber/t3638ED71-98B8-408A-B640-D25063C79E763> ; data: {
    contact = "0x6080000369c0 <x-coredata:///Contact/t3638ED71-98B8-408A-B640-D25063C79E762>";
    number = "";
    speedDial = "-";
    type = Other;
})
)}, NSAffectedObjectsErrorKey=(
    "<ContactNumber: 0x6080000abb20> (entity: ContactNumber; id: 0x608000037920 <x-coredata:///ContactNumber/t3638ED71-98B8-408A-B640-D25063C79E763> ; data: {\n    contact = \"0x6080000369c0 <x-coredata:///Contact/t3638ED71-98B8-408A-B640-D25063C79E762>\";\n    number = \"\";\n    speedDial = \"-\";\n    type = Other;\n})"
), NSValidationErrorObject=<Contact: 0x6080000d2210> (entity: Contact; id: 0x6080000369c0 <x-coredata:///Contact/t3638ED71-98B8-408A-B640-D25063C79E762> ; data: {
    company = 44;
    contactNumbers =     (
        "0x608000037920 <x-coredata:///ContactNumber/t3638ED71-98B8-408A-B640-D25063C79E763>"
    );
    defaultNumber = 0;
    firstName = 11;
    isFavorite = 0;
    isGroupHeader = 0;
    lastName = 33;
    middleInitial = 22;
}), NSLocalizedDescription=contactNumbers is not valid., NSValidationErrorKey=contactNumbers, NSValidationErrorShouldAttemptRecoveryKey=true}

请帮助我。

1 个答案:

答案 0 :(得分:0)

错误1150表示您有核心数据验证错误。因此,您试图保存与数据模型规范相矛盾的内容。

我建议您浏览所有实体和属性,检查哪些是可选的,是否存在关系数量限制,值是否有范围等。

记录所有已更改的对象并比较这些规则。保存时生成的错误对象实际上应该提供更多可操作的信息。

也可能是您将同一个对象多次添加为同一个父对象的关系。

&#34;悬挂参考&#34;意味着你的对象图有误。这可能是因为可能没有正确设置反向关系。

从您发布的代码中:确保将新创建的联系人号码分配给联系人实体。