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)
"ContactNumber" Object model:
Their 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.


