Core Data保存崩溃 [英] Core Data save crashes

查看:222
本文介绍了Core Data保存崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableView 使用 FetchedResultsController CoreData $ c>并注册数据更新。

I have a UITableView that fetches data from CoreData using FetchedResultsController and it registers for the data update.

在第二个线程,我从服务器下载数据并更新相同的数据c $ c> UITableView )。更新并不复杂,它只是更新实体的BOOL字段。

On a second thread, I download the data from the server and update the same data (that's used by the UITableView). The update is not complicated and It is just updating a BOOL field of the entity.

当我调用对象上下文保存时,我得到这个异常: NSInternalInconsistencyException ,原因是

When I call the save on Object Context, I get this exception: NSInternalInconsistencyException and the reason is

"Failed to process pending changes before save.  The context is still dirty after 100 attempts. ..."

如果我在更新后不保存,在应用程序即将终止的时候,应用程序运行正常,并且 UITableView 已正确更新,并且数据已保留。

If I do not save right after the update but only at the time when the application is about to terminate, the application runs fine and the UITableView is correctly updated and the data is persisted.

任何指向为什么可能发生?我做错了什么?

Any pointer on why that might be happening? Am I doing something wrong?

推荐答案

托管对象上下文不是线程安全的。每个线程都有单独的 MOC

Managed object contexts are not thread safe. Do you have a separate MOC for each thread?

如果是这样,我相信正确的模式是注册 MOC 的背景中创建 NSManagedObjectDidSaveNotifications ,以便您可以执行 mergeChangesFromContextDidSaveNotification 在主程序 MOC (从主线程)。这将保持您的 MOCs 同步;它不会自动发生。

If so, I believe the correct pattern is to register for NSManagedObjectDidSaveNotifications from the background MOC such that you can do a mergeChangesFromContextDidSaveNotification on the main MOC (from the main thread). This will keep your MOCs in sync; it does not happen automatically.

这篇关于Core Data保存崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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