UIManagedDocument does not save

I am using UIManagedDocument to manage my data. I create a model and use it, and everything seems to work, but my changes are not written to the SQLite repository.

The documentation for UIManagedDocument says that autosave should take care of storing data in the database, but this does not seem to happen.

NSManagedObjectContext *moc = [doc managedObjectContext]; NSError *error = nil; MyItem *itemToAdd = (MyItems *)[moc existingObjectWithID:(NSManagedObjectID *)itemsID error:&error]; 

Selects the object that I want to add (and successfully).

  [itemContainer addItemsObject:itemToAdd]; [doc updateChangeCount:UIDocumentChangeDone]; 

This adds the item to the collection of items in another object, and then tells the document that I made the changes.

I expect some time soon afterwards to see the changes recorded in the Core Data repository, but looking in the "Tools", I see that this will never happen.

The collection of elements is an NSOrderedSet and due to comments on this element:

Exception thrown in NSOrderedSet-generated accessories

I added addItemsObject: to the object that contains the collection:

 - (void)addItemsObject:(MyItem *)value { NSMutableOrderedSet* tempSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.items]; [tempSet addObject:value]; self.items = tempSet; } 

It is possible that something is wrong when Core Data reports that the collection of elements has changed, but I donโ€™t see how to do it.

+7
source share
3 answers

I found my problem. It turns out I had an error with the object I was trying to add - I skipped the required property - and without overriding handleError there is no indication of a problem.

This is reported here: http://blog.stevex.net/2011/12/uimanageddocument-autosave-troubleshooting/

+9
source

In my method, where I get data from the server, I first create Objects, and after that I call these two methods to immediately save changes to the document:

 [self.document updateChangeCount:UIDocumentChangeDone]; [self.document savePresentedItemChangesWithCompletionHandler:^(NSError *errorOrNil) { ... }]; 
+1
source

Key take aways / summary from @stevex link:

Be sure to call the UIManagedDocument -updateChangeCount method or call the change registered in the undoManager document. Otherwise, the document does not consider it necessary to save anything.

In addition, subclassing some key methods will allow you to see when autosave occurs, and if there are errors.

 - (id)contentsForType:(NSString *)typeName error:(NSError * _Nullable __autoreleasing *)outError { id retVal = [super contentsForType:typeName error:outError]; NSLog(@"Autosaving document. contentsForType at fileURL %@ error %@", self.fileURL, *outError); return retVal; } - (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted { [super handleError:error userInteractionPermitted:userInteractionPermitted]; NSLog(@"ManagedDocument handleError: %@ %@", error.localizedDescription, error.userInfo); } 
0
source

All Articles