我们如何防止“CoreData 无法完成故障”?

发布于 2024-11-28 06:46:12 字数 363 浏览 0 评论 0原文

我们偶尔会收到“CoreData 无法完成故障”的信息。我们已阅读 Apple 文档,但不清楚允许保留哪些内容。我们非常小心地为每个线程创建一个上下文等。但是,我们的应用程序正在做的一件事是我们在 UIViewController 上保留 NSManagedObjects(通常通过 NSArray 或 NSDictionary)。我猜发生的事情是对象关系正在发生变化,而我们没有处理适当的通知。

有人对核心数据更好的设计有什么建议吗?当我们收到错误时,我看不到我们实际上从上下文中删除了任何内容来导致错误。如果 UIViewController 保留状态,是否有必要处理 NSManagedObjectContextObjectsDidChangeNotification?任何建议将不胜感激。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2024-12-05 06:46:12

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

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

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

- (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];  
}

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)];

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];  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文