0
votes

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

More details:

I have a next model:("Contact" Object model)

Contact Object model

"ContactNumber" Object model:

ContactNumber" Object model

Their relationships:

relationships

I have a custom NSView class with property:

@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":

-(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. 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.

1

1 Answers

0
votes

Error 1150 means that you have a Core Data validation error. 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.