Why does Core Data take so long to save an object?

雨燕双飞 提交于 2019-12-06 16:09:12

Easy fix, I now always save the NSManagedDocument after any changes to the context (I don't need undo functionality).

In my shared document handler class, I tell it to save on change notification.

- (void)objectsDidChange:(NSNotification *)notification
{
    #ifdef DEBUG
        NSLog(@"NSManagedObjects did change.");
    #endif
    [BSDocumentHandler saveDocument];
}

And saveDocument is just a class method:

+ (void)saveDocument
{
    UIManagedDocument *document = [[BSDocumentHandler sharedDocumentHandler] document];
    [document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:nil];
}

It now saves immediately and when I stop the simulator using stop, it now preserves the saves.

Furthermore, to avoid duplicate saves, I now removed all lines containing a managed object context save after creating, editing or deleting an object, and my shared document handler class manages all saves automatically, making for much less duplicated code.

My log is now much cleaner. This is performing a deletion. I invoke the VC that lists all items, swipe to delete, and confirm. It notices there was a change to the MOC and saves the document once (as expected).

2013-05-30 13:33:01.310 App[27167:c07] [ItemCDTVC setFetchedResultsController:] set
2013-05-30 13:33:01.312 App[27167:c07] [ItemCDTVC performFetch] fetching all Item (i.e., no predicate)
2013-05-30 13:33:04.735 App[27167:c07] NSManagedObjects did change.
2013-05-30 13:33:04.751 App[27167:c07] NSManagedContext did save.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!