UIManagedDocument and NSFetchedResultsController
I'm having issues with saving an entity using a UIManagedDocument. I have a NSFetchedResultsController with its context set as the UIManagedDocuments context. I have the controller set up with sections. The user adds an entry to the UIManagedDocuments context and I save the context using: [context save:&error]; and my NSFetchedResultsController updates properly.
If I leave the view that has the NSFetchedResultsController and then return to the view, in which I create a new NSFetchedResultsController with the same UIManagedDocuments context, I get an error back when I call: [fetchedResultsController performFetch:&error];
The error says:
CoreData: error: (NSFetchedResultsController) The fetched object at index has an out of order section name '. Objects must be sorted by section name'
and the fetch fails.
If I wait a while before I return to the view I don't get the error. I know that the save operation is done on the child context and then pushes the changes onto the parent context and I think this has something to do with the problem. Does anyone have a solution?
According to the UIManagedDocument Reference, you should not save via the NSManagedObjectContext. Saving should be done via the appropriate UIManagedDocument API(s). Unfortunately, the document is not exactly clear on what you should use.
If you use the undo manager, that path is supposed to make sure everything is appropriately marked as dirty, and saved. Likewise, using an explicit call to
[document updateChangeCount:UIDocumentChangeDone];
is supposed to provide similar functionality. However, saving will be postponed because it is done in a separate thread.
链接地址: http://www.djcxy.com/p/36182.html