简体   繁体   English

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

[英]Temporary object for Managed Object Context

I have trying to create and update temporary NSManagedObject and after that insert it to Managed Object Context for saving, but on saving state I have receive an error Error = Error Domain=NSCocoaErrorDomain Code=1550 我试图创建和更新临时NSManagedObject,然后将其插入到Managed Object Context中进行保存,但是在保存状态下,我收到错误Error = Error Domain=NSCocoaErrorDomain Code=1550

More details: 更多细节:

I have a next model:("Contact" Object model) 我有一个下一个模型:(“ Contact”对象模型)

接触对象模型

"ContactNumber" Object model: “ ContactNumber”对象模型:

ContactNumber“对象模型

Their relationships: 他们的关系:

关系

I have a custom NSView class with property: 我有一个带有属性的自定义NSView类:

@property (nonatomic, strong) Contact *selectedContact;

In this class I have a button "Add contact". 在本课程中,我有一个“添加联系人”按钮。 When I press it I create a new temporary object "Contact": 当我按下它时,我将创建一个新的临时对象“ Contact”:

-(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];
} 

And add to this object new "contact Number": 并向该对象添加新的“联系电话”:

-(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];
}

After some manipulation with _selectedContact object I need to save it in the my MOC. 使用_selectedContact对象进行一些操作后,我需要将其保存在我的MOC中。 I do next: 接下来,我要做:

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

And I have receive next 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}

Please help me with this. 请帮我解决一下这个。

Error 1150 means that you have a Core Data validation error. 错误1150表示您有核心数据验证错误。 So you are trying to save something that is contradicting the specifications of your data model. 因此,您正在尝试保存与数据模型的规范相矛盾的内容。

I would recommend you go through all your entities and attributes and check for which ones are optional or not, if there is a limit to the number of relationships, if the values have ranges, etc. 我建议您仔细检查所有实体和属性,并检查哪些实体和属性是可选的,是否对关系数量有限制,值是否具有范围等。

Log all the changed objects and compare these rules. 记录所有更改的对象并比较这些规则。 The error object generated when saving should actually give some more actionable information. 保存时生成的错误对象实际上应提供更多可操作的信息。

It could also be you are adding the same object more than once as a relationship to the same parent object. 也可能是您多次将同一对象作为与同一父对象的关系添加。

"Dangling reference" means that you have a mistake to your object graph. “悬挂引用”表示您对对象图有误。 That could be that maybe a reverse relationship is not set correctly. 可能是因为反向关系设置不正确。

From the code you posted: make sure you assign your newly created contact number to the Contact entity. 从您发布的代码中:确保将新创建的联系人号码分配给联系人实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM