为什么核心数据保存对象需要这么长时间? [英] Why does Core Data take so long to save an object?

查看:70
本文介绍了为什么核心数据保存对象需要这么长时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实体类别中有一个简单的方法,该方法将自定义方法添加到我的子类 NSManagedObject 类中(由于自动生成子类,因此我不会碰它)。



我有这种方法可以从MOC中删除对象并保存上下文。

  +(void)deleteWishlist:(Wishlist *)wishlist 
inManagedObjectContext:(NSManagedObjectContext *)上下文
{

[context deleteObject:wishlist];

NSError *错误;
if(![context save:& error]){
NSLog(@%@,error);
} else {
NSLog(@删除成功);
}
}

我也是从我的一个视图控制器中调用它的:

 -(void)delete心愿单:(心愿单*)心愿单
{
if(心愿单){
[收藏夹删除收藏夹:收藏夹中的收藏夹:self.managedObjectContext];
}
}

它是通过UI事件处理方法调用的(委托 tableView:commitEditingStyle:forRowAtIndexPath:方法)。



现在,这一切都可行,但是要花很长时间保存对象...我不知道这是否是默认行为,但是我希望它在从图表中删除后立即立即保存到持久性存储中。我不在乎是否要等到它保存下来。



在iOS模拟器上,如果我按下停止然后再次加载我的应用,则愿望清单仍然存在。如果我按下主屏幕按钮,则该应用会进入后台模式,并在那里被保存...但是好像它已被保存了两次。老实说,我不知道它在做什么。

  2013-05-29 00:45:14.819 App [29024:c07] [WishlistCDTVC setFetchedResultsController:]设置
2013-05-29 00:45:14.820应用[29024:c07] [WishlistCDTVC performFetch]获取所有Wishlist(即,没有谓词)
2013- 05-29 00:45:15.755 App [29024:c07]删除已按下的
2013-05-29 00:45:16.666 App [29024:c07] NSManagedObjects确实发生了变化。
2013-05-29 00:45:16.669 App [29024:c07] NSManagedContext确实保存了。
2013-05-29 00:45:16.670 App [29024:c07]成功删除
2013-05-29 00:45:19.119 App [29024:c07] NSManagedContext确实保存了。

我的工作:加载愿望清单VC,滑动以删除,确认,并说按删除'。从上下文中删除对象,保存对象(但显然不在这里),然后说删除成功(所以没有错误),然后我按下home按钮,它记录了最后一个保存行,最终保存到永久存储。那么中间为什么不省钱呢?

解决方案

容易修复,我现在总是保存 NSManagedDocument 对上下文进行任何更改后(我不需要撤消功能)。



在共享文档处理程序类中,我告诉它保存

 -(void)objectsDidChange:(NSNotification *)notification 
{
#ifdef调试
NSLog(@ NSManagedObjects确实发生了更改。);
#endif
[BSDocumentHandler saveDocument];
}

saveDocument 为只是一个类方法:

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

现在可以立即保存,当我使用stop停止模拟器时,现在可以保存保存。



此外,为避免重复保存,我现在在创建,编辑或删除对象后删除了所有包含托管对象上下文保存的行,而我的共享文档处理程序类管理所有自动保存,从而减少了重复的代码。



我的日志现在更干净了。这正在执行删除。我调用列出所有项目的VC,滑动以删除并确认。它注意到MOC发生了更改,并一次保存了文档(如预期)。

  2013-05-30 13: 33:01.310 App [27167:c07] [ItemCDTVC setFetchedResultsController:]设置
2013-05-30 13:33:01.312 App [27167:c07] [ItemCDTVC performFetch]获取所有项目(即,没有谓词)
2013-05-30 13:33:04.735 App [27167:c07] NSManagedObjects确实发生了变化。
2013-05-30 13:33:04.751 App [27167:c07] NSManagedContext确实保存了。


I have a simple method in my entity category that adds custom methods to my subclassed NSManagedObject classes (which I don't touch because of automatic subclassing generating).

I have this method that deletes an object from the MOC and saves the context.

+ (void)deleteWishlist:(Wishlist *)wishlist
    inManagedObjectContext:(NSManagedObjectContext *)context
{

    [context deleteObject:wishlist];

    NSError *error;
    if(![context save:&error]) {
        NSLog(@"%@", error);
    } else {
        NSLog(@"Delete successful");
    }
}

I invoke this from one of my view controllers as so:

- (void)deleteWishlist:(Wishlist *)wishlist
{
    if(wishlist) {
        [Wishlist deleteWishlist:wishlist inManagedObjectContext:self.managedObjectContext];
    }
}

It gets called from a UI event handling method (the delegate tableView: commitEditingStyle: forRowAtIndexPath: method).

Now, this all works, but it's taking a long time to save the object...I don't know if this is the default behavior, but I want it to save immediately to persistent storage after deleting from the graph. I don't mind if I have to wait until it saves.

On the iOS simulator, if I hit 'stop' and then load my app back up again, the wishlist is still there. If I hit the home button, the app goes to background mode, and there it gets saved...but it seems like it's saving it twice. I don't know what it's doing the first time to be honest.

2013-05-29 00:45:14.819 App[29024:c07] [WishlistCDTVC setFetchedResultsController:] set
2013-05-29 00:45:14.820 App[29024:c07] [WishlistCDTVC performFetch] fetching all Wishlist (i.e., no predicate)
2013-05-29 00:45:15.755 App[29024:c07] Delete pressed
2013-05-29 00:45:16.666 App[29024:c07] NSManagedObjects did change.
2013-05-29 00:45:16.669 App[29024:c07] NSManagedContext did save.
2013-05-29 00:45:16.670 App[29024:c07] Delete successful
2013-05-29 00:45:19.119 App[29024:c07] NSManagedContext did save.

What I did: load up the wishlist VC, swipe to delete, confirm, it said 'delete pressed'. Deletes object from context, saves it (but apparently not here), then says delete successful (so no error), and then I hit the home button and it logs the last 'save' line, which DOES finally save to persistent storage. So why isn't the middle one saving?

解决方案

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.

这篇关于为什么核心数据保存对象需要这么长时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆