NSFetchedResultsController不显示来自其他上下文的更新 [英] NSFetchedResultsController not showing updates from other contexts

查看:117
本文介绍了NSFetchedResultsController不显示来自其他上下文的更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSFetchedResultsController设置填充一个UITableView,基于我的'main'NSManagedObjectContext。



在一个计时器,我不断地添加对象到一个单独的NSManagedObjectContext,但是一个与主上下文共享其NSPersistantStoreCoordinator。



我可以看到NSManagedObjectContextWillSaveNotification通知被发送,当我退出重新启动应用程序,我有所有的数据从上次运行。但是,当前的NSFetchedResultsController没有更新。



我创建了一个测试项目,并将它放置 此处



>

(我最初认为这是因为有多个线程在玩;但是,当一切都在一个线程中发生时)。

原始问题



我有一个NSFetchedResultsController设置为填充UITableView, 'NSManagedObjetContext。



在后台线程中,我下载并导入几个对象到一个单独的NSManagedObjectContext,但是一个与主线程的上下文共享它的NSPersistantStoreCoordinator。

我注册接收NSManagedObjectContextWillSaveNotification通知。当我得到这些,我将它们转发到主线程,并将它们传递给我的主上下文。



使用调试器,我可以看到这些进来,他们确实是在上下文中添加对象。但是,我的NSFetchedResultsController没有更新。如果我做这一切在主线程,它的工作。调用-performFetch:在控制器上将导致它更新,所以我知道这不是没有看到新的对象。



大多数是模板代码,只是split


解决方案

是否从NSFetchedResultsController获取任何委托回调?如果不是,那么这可能是NSFetchedResultsController本身的错误。我建议创建一个测试项目,可以单独确认/拒绝此问题。



如果您可以隔离复制它;


  1. 我很高兴看到它:)

  2. 用苹果>

查看代码后,答案是将您的通知观察器从 NSManagedObjectContextWillSaveNotification 更改为 NSManagedObjectContextDidSaveNotification 。您在之前发送的邮件是导致您出现问题的保存。


I have an NSFetchedResultsController set up to populate a UITableView, based off my 'main' NSManagedObjectContext.

In a timer, I continually add objects into a separate NSManagedObjectContext, but one that shares its NSPersistantStoreCoordinator with the main context.

I can see NSManagedObjectContextWillSaveNotification notifications being sent, and when I quit an restart the app, I have all the data from the previous run. However, no updates happen to the current NSFetchedResultsController.

I've created a test project and placed it here.



(I initially thought this was happening due to there being multiple threads at play; however, it happens when everything is done in one thread).

Original Question

I have an NSFetchedResultsController set up to populate a UITableView, based off my 'main' NSManagedObjetContext.

In a background thread, I download and import several objects into a separate NSManagedObjectContext, but one that shares its NSPersistantStoreCoordinator with the main thread's context.

I am registered to receive NSManagedObjectContextWillSaveNotification notifications. When I get these, I forward them on to the main thread, and pass them to my main context.

Using the debugger, I can watch these come in, and verify that they are, indeed, adding objects to the context. However, my NSFetchedResultsController is not updating. If I do this all in the main thread, it does work. Calling -performFetch: on the controller will cause it to update, so I know it's not that it's not seeing the new objects.

Most of this is boilerplate code, just split into different threads.

解决方案

Are you getting any delegate call backs from the NSFetchedResultsController? If not then this may be a bug with the NSFetchedResultsController itself. I would recommend creating a test project that can confirm/deny this issue in isolation.

If you can duplicate it in isolation;

  1. I would love to see it :)
  2. File a radar with Apple.

After reviewing the code, the answer is to change your notification observer from NSManagedObjectContextWillSaveNotification to NSManagedObjectContextDidSaveNotification. You were sending the message just before the save occurred which was causing your issues.

这篇关于NSFetchedResultsController不显示来自其他上下文的更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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