13

Currently all of my saves are going to memory but are not being written out to disk (iOS). My app is set up with a UITableView with an Add Modal View presented over this to create content, when the user is done creating the content and the save button is clicked the new Item (NSManagedObject class created by my CoreData Model) I print it out and it is fully filled in. Immediately after this I try to save it to disk and an error message is produced with the same object ID except the fields are nil. In between however my UITableViews - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath method is getting called which is logging @"CanEdit".

Can anyone see what I am doing wrong?

Here is the code

        NSLog(@"newItem %@", newItem);
    NSError *error;
    if (![newItem.managedObjectContext save:&error]) {
        // Handle the error.
        NSLog(@"%@", error);
    }

    if (editItem) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self dismissModalViewControllerAnimated:YES];            
    }

And Here is my error

2011-10-22 15:24:46.322 App[42115:fb03] newItem <Item: 0x81a4a30> (entity: Item; id: 0x81a0ab0 <x-coredata:///Item/t7F2B54D2-0DCC-4530-88D5-900BE25C7DC23> ; data: {
    containedIn = "0x6e89010 <x-coredata:///Item/t7F2B54D2-0DCC-4530-88D5-900BE25C7DC22>";
    contains =     (
    );
    content = a;
    dateLastUsed = nil;
    depth = 0;
    encrypted = 0;
    favorite = 0;
    favoritePosition = nil;
    folder = 0;
    fullPath = "^Templates^Add Title";
    name = a;
    sortPosition = 0;
})
2011-10-22 15:24:46.323 App[42115:fb03] CanEdit
2011-10-22 15:24:46.326 App[42115:fb03] Error Domain=NSCocoaErrorDomain Code=1570 "The operation couldn’t be completed. (Cocoa error 1570.)" UserInfo=0x6ecc490     
{NSValidationErrorObject=<Item: 0x6e88fb0> (entity: Item; id: 0x6e89010 <x-coredata:///Item/t7F2B54D2-0DCC-4530-88D5-900BE25C7DC22> ; data: {
    containedIn = nil;
    contains =     (
        "0x81a0ab0 <x-coredata:///Item/t7F2B54D2-0DCC-4530-88D5-900BE25C7DC23>"
    );
    content = nil;
    dateLastUsed = nil;
    depth = 0;
    encrypted = 0;
    favorite = 0;
    favoritePosition = nil;
    folder = 1;
    fullPath = "^Templates^";
    name = Templates;
    sortPosition = 0;
}), NSValidationErrorKey=content, NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1570.)}
beryllium
  • 29,214
  • 15
  • 99
  • 123
xizor
  • 1,486
  • 2
  • 16
  • 34
  • possible duplicate of [iphone Core Data Unresolved error while saving](http://stackoverflow.com/questions/1283960/iphone-core-data-unresolved-error-while-saving) – millimoose Oct 22 '11 at 19:55
  • I saw that earlier, I know this error is due to a required field not being set but my question is why does it go from being set to not being set immediately afterwards? – xizor Oct 22 '11 at 20:20
  • The error message is for a different entity (`0x81a0ab0`) than the one you're logging (`0x6e89010`), and it seems like one contains the other. Does Core Data support cascading? If so, it means you're really saving two objects with one call, and the container object's attributes are nil. – millimoose Oct 22 '11 at 20:25
  • Posterity, know that a stray synthesize in your custom managed object subclass can also do this to you. – Manav May 17 '13 at 05:28

2 Answers2

29

The problem is that you have a MO in your context which has required fields set to nil. Specifically this is saying NSValidationErrorKey=content which in the preceding NSValidationErrorObject is printing out as nil.

Either you have a logic error where you values are not getting set in the MO correctly, or you should change your model to make that field optional.

logancautrell
  • 8,762
  • 3
  • 37
  • 50
  • 1
    Thank you, I found out earlier that that is what the error code means the only question is why is my object changing from content=a and name=a in the first log to content=nil and name=nil the log of the error message and they occur right after each other. I am setting them with newItem.name = @"text here" - is this not the correct way? – xizor Oct 22 '11 at 20:19
  • 2
    That is correct. It looks like you may have another object being created that is not being populated correctly. Double check that you are only creating the number of objects you are expecting. Set a break point in the subclass's init method. – logancautrell Oct 22 '11 at 20:25
  • 1
    Thanks, that was the problem I inadvertently created another object. – xizor Oct 23 '11 at 20:30
  • in my case i was adding a MO as property to another MO mulitple times but the relation was defined as one to one from both sides which was causing validation violation error in saveContext. Setting the relation to one-to-many fixed the problem. – Ammar Mujeeb Nov 12 '18 at 12:44
1

From your error output above, you can see that there are two different objects, one with the address 0x6e89010 containing your data, another with the address 0x6e88fb0 where required fields are nil.

The source of this error must be contained in code that you did not post.

My recommendation in order to avoid these kind of problems is to follow the following design pattern which is also used in Apple's demos:

  • Pass the managed object context as a property to the modal view controller. It is advisable to only have one managed object context.
  • Create a new managed object when the input controller starts with [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self.managedObjectContext];
  • As the user inputs the data, assign the properties / attributes to your new object right away.
  • When the user hits "Save", save the changes with [self.managedObjectContext save:&error];
  • If the user cancels, delete the object from the context with [self.managedObjectContext deleteObject:insertedObject];

This is very efficient and tends to avoid stray object errors.

Mundi
  • 77,414
  • 17
  • 110
  • 132