objective-ccore-dataios5uimanageddocument

UIManagedDocument not saving


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.


Solution

  • 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/