简体   繁体   English

NSFetchedResultsController和NSOperation

[英]NSFetchedResultsController and NSOperation

In a UITableViewController , I use an NSFetchedResultsController for my data. UITableViewController ,我使用NSFetchedResultsController作为我的数据。 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 . 一切工作正常,除非我开始导入一个单独的线程某些对象为:我使用一个NSOperationQueue我在其中插入异物我ManagedObjectContext This happens in a separate view. 这在单独的视图中发生。 The NSFetchedResultsController doesn't seem to like this and writes to the console: NSFetchedResultsController似乎不喜欢这样并写入控制台:

Serious application error. 严重的应用错误。 An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. 在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常。 Attempt to create two animations for cell with userInfo (null) 尝试使用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. 关于并发性主题,“ 核心数据编程指南”说的是为每个线程使用ManagedObjectContext ,但听起来相当复杂。

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? 我现在不知道我是否真的应该创建自己NSOperation子类,创建一个ManagedObjectContext在里面等,还是可以防止NSFetchedResultsController从更新了一段时间?

I would appreciate some help, Fabian Fabian,我很感激一些帮助

You need a NSManagedObjectContext per thread, sorry! 你需要每个线程一个NSManagedObjectContext ,对不起!

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. 不只是NSFetchesResultsController将访问您的上下文 - coreData将不会获取某些数据,直到您的上下文需要在任何时候访问。

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. 只需在您的委托上编写一个方法来创建一个托管对象上下文并在每个NSOperations中调用它 - 这将使每个线程而不是全部使用相同的线程。

The managed context on your main thread can be created with this method as well. 您也可以使用此方法创建主线程上的托管上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM