我们如何防止“CoreData无法完成故障”? [英] How do we prevent "CoreData could not fulfill a fault"?

查看:134
本文介绍了我们如何防止“CoreData无法完成故障”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们每隔一段时间就得到CoreData无法完成故障。我们已经阅读了苹果文档,但不清楚什么是允许保留。我们一直非常小心为每个线程创建一个上下文等。然而,我们的应用程序正在做的一件事是我们保留NSManagedObjects在我们的UIViewControllers(通常通过NSArray或NSDictionary)。我猜想发生了什么是对象关系正在改变,我们不处理适当的通知。

We get "CoreData could not fulfill a fault" every once in a while. We have read through the Apple documentation but are unclear on what is allowed to be retained. We have been very careful about creating one context per thread, etc. However, one thing our app is doing is we are retaining NSManagedObjects on our UIViewControllers (usually via a NSArray or NSDictionary). I'm guessing what's going on is the object relationships are changing and we are not handling the appropriate notification.

有没有人对核心数据更好的设计有任何建议?当我们得到错误,我不能看到我们实际上删除了上下文中的任何东西,造成的错误。是否有必要处理NSManagedObjectContextObjectsDidChangeNotification在我们的UIViewControllers如果他们保持状态?任何建议将不胜感激。

Does anyone have any suggestions on the better design with regards to Core Data? When we get the error, I cannot see that we actually deleted anything from the context to cause the fault. Is it necessary to handle NSManagedObjectContextObjectsDidChangeNotification on our UIViewControllers if they are retaining state? Any suggestions would be appreciated.

推荐答案

您可以在Core Data中注册变更通知。这将允许您在托管对象更改时更新它们。有关详细信息,请参阅Core Data文档。您将对2种注册和响应更改的方法感兴趣:

You can register for change notifications in Core Data. This will allow you to update your managed objects when they change. See the Core Data Docs for more info. You're going to be interested in 2 methods to register and respond to changes:

  [NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(mergeChanges:)
                                              name:NSManagedObjectContextDidSaveNotification
                                            object:(your NSManagedObjectContext)];

mergeChanges选择器(您的方法)将调用以下方法来同步其他线程的任何更改。它将看起来像这样:

The mergeChanges selector (your method) will call the following method to synchronize any changes from other threads. It will look something like this:

- (void)mergeChanges:(NSNotification *)notification{
  AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
  NSManagedObjectContext *context = [appDelegate managedObjectContext];

  // Merge changes into the default context on the main thread
  [context performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
                            withObject:notification
                         waitUntilDone:YES];  
}

这篇关于我们如何防止“CoreData无法完成故障”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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