CoreData Error Code 1570

Currently, all my saves go to memory, but are not written to disk (iOS). My application is configured using a UITableView with the Add Modal View text above it to create content when the user has finished creating the content and the Save button clicked on a new element (NSManagedObject class created by my CoreData model). I print it is completely full. Immediately after that, I try to save it to disk, and an error message is generated with the same object identifier, with the exception of fields equal to zero. Meanwhile, my UITableViews are (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath method that registers @"CanEdit" called.

Can anyone see what I'm 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 mistake

 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.)} 
+7
source share
2 answers

The problem is that you have MO in your context that has the required fields set to nil. In particular, this says NSValidationErrorKey=content , which in the previous NSValidationErrorObject is printed as nil.

Either you have a logical error when your values ​​are not set correctly in MO, or you must change your model to make this field optional.

+17
source

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

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

My recommendation to avoid such problems is the following design pattern, which is also used in Apple demos:

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

It is very efficient and tends to avoid errors in object objects.

0
source

All Articles