NSFetchedResultsController 和 NSOperation

发布于 2024-10-07 21:15:16 字数 784 浏览 0 评论 0原文

UITableViewController 中,我使用 NSFetchedResultsController 来存储数据。一切工作正常,除了当我开始在单独的线程中导入一些对象时:我使用 NSOperationQueue,在其中将对象插入到我的 ManagedObjectContext 中。这发生在单独的视图中。 NSFetchedResultsController 似乎不喜欢这样并写入控制台:

严重的应用程序错误。在调用 -controllerDidChangeContent: 期间,从 NSFetchedResultsController 的委托捕获了异常。尝试为带有 userInfo (null) 的单元格创建两个动画

显然它尝试获取新对象。

关于并发主题,核心数据编程Guide 说类似为每个线程使用 ManagedObjectContext ,但这听起来相当复杂。

我现在不知道是否应该实际创建自己的 NSOperation 子类,在其中创建 ManagedObjectContext 等等,或者是否可以阻止 NSFetchedResultsController 已经更新了一段时间了?

我希望得到一些帮助,法比安

In a UITableViewController, I use an NSFetchedResultsController for my data. Everything works fine, except for when I start importing some objects in a separate thread: I use an NSOperationQueue in which I insert objects into my ManagedObjectContext. This happens in a separate view.
The NSFetchedResultsController doesn't seem to like this and writes to the console:

Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Attempt to create two animations for cell with userInfo (null)

Apparently it tries to fetch the new objects.

On the topic of concurrency, the Core Data Programming Guide says something like using a ManagedObjectContext for each thread, but that sounds rather complicated.

I now don't know whether I should actually create my own NSOperation subclass, creating a ManagedObjectContext in it and so on, or whether it is possible to prevent the NSFetchedResultsController from updating for some time?

I would appreciate some help, Fabian

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

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

发布评论

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

评论(1

朮生 2024-10-14 21:15:16

每个线程都需要一个 NSManagedObjectContext ,抱歉!

不仅仅是 NSFetchesResultsController 将访问您的上下文 - coreData 不会获取某些数据,直到您的上下文可能在任何时候访问它。

但是,这只是您需要在每个线程的基础上创建的上下文。只需在委托上编写一个方法来创建托管对象上下文,并在每个 NSOperations 中调用该方法 - 这将使它们成为每个线程,而不是全部使用相同的线程。

主线程上的托管上下文也可以使用此方法创建。

You need a NSManagedObjectContext per thread, sorry!

It's not just the NSFetchesResultsController that will be accessing your context - coreData won't fetch some data until it's needed to your context might be accessed at any point.

However, it's only the context that you need to create on a per thread basis. Just write a method on your delegate that creates a managed object context and call that in each of your NSOperations - this will make them per thread instead of all using the same one.

The managed context on your main thread can be created with this method as well.

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