7

I'm using UIManagedDocument to manage my data. I create the model and use it, and everything seems to be working, but my changes aren't being written back to the SQLite store.

The documentation for UIManagedDocument says that the autosave should take care of persisting the data to the database, but that doesn't seem to be happening.

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

This fetches the object I want to add (and succeeds).

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

This adds the item to an items collection in another object, and then tells the document that I'm done making changes.

I'd expect some time shortly after this to see the change written to the Core Data store, but watching in Instruments, I see that it never happens.

The items collection is an NSOrderedSet, and because of comments on this item:

Exception thrown in NSOrderedSet generated accessors

I've added an addItemsObject: to the object that holds the collection:

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

Perhaps something is going wrong with Core Data being informed that the items collection has changed, but I don't see how.

Community
  • 1
  • 1
stevex
  • 5,055
  • 30
  • 49
  • With Instruments, I can see that data is being written to the Core Data Cache. But it doesn't show up in the Core Data Saves instrument, and if I kill and restart the app, my changes are gone. – stevex Dec 03 '11 at 14:53
  • 3
    I found my problem. Turns out I had an error with the object I was attempting to add - I missed a required property - and without overriding handleError there's no indication that there's a problem. Blogged about it here: http://blog.stevex.net/2011/12/uimanageddocument-autosave-troubleshooting/ – stevex Dec 04 '11 at 03:35

3 Answers3

10

I found my problem. Turns out I had an error with the object I was attempting to add - I missed a required property - and without overriding handleError there's no indication that there's a problem.

Blogged about it here: http://blog.stevex.net/2011/12/uimanageddocument-autosave-troubleshooting/

stevex
  • 5,055
  • 30
  • 49
  • 1
    this is a terrific find - the fact that CoreData in many cases will allow objects to be persisted in memory, but not save to disk can be *extremely* misleading at times, and the fix you have given is great. – NSTJ Mar 09 '13 at 16:08
  • I put up an answer that summarizes your link in case of future link failure :) – olynoise Oct 23 '17 at 21:33
1

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

[self.document updateChangeCount:UIDocumentChangeDone];
[self.document savePresentedItemChangesWithCompletionHandler:^(NSError *errorOrNil) {
            ...
        }];
carmen_munich
  • 5,878
  • 1
  • 32
  • 39
1

Key take aways / summary from @stevex's link :

Make sure to call the UIManagedDocument's -updateChangeCount method or trigger a change that is registered with the document's undoManager. Otherwise the document doesn't think it needs to save anything.

Also, subclassing some key methods will allow you to see when autosaving 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);
}
olynoise
  • 1,936
  • 2
  • 16
  • 32