NSFetchedResultsController是否会观察对持久存储的所有更改? [英] Does NSFetchedResultsController Observe All Changes to Persistent Store?

查看:115
本文介绍了NSFetchedResultsController是否会观察对持久存储的所有更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的计划的工作方式如下:



更新NSFetchedResultsController的结果而不进行新的抓取


  1. 显示NSFetchedResultsController的结果到UITableView

  2. 从web服务获取新对象并将其存储到核心数据(在同一视图控制器中,使用RestKit)

  3. 通过NSFetchedResultsController delegate

NSFetchedResultsControllerDelegate的实现是从Apple的Core Data项目中复制的,我的预测是:

  [NSPredicate predicateWithFormat:@isMyTest == TRUE] 

如果属性更新从TRUE更改为FALSE,它从tableview中删除行(因为该行的对象位于NSFetchedResultsController的 fetchedObjects 中) p>

但是,如果属性更新从FALSE变为TRUE,NSFetchedResultsController不会通知任何内容,因此无法在表视图中看到新数据。如果我手动更新 BOTH NSFetchedResulsController和UITableView,它会显示新数据。



我以为NSFetchedResultController会监视持久存储中的所有更改,是否太大了希望? :D



(我真的想这样做,因为其他视图控制器可以更新持久存储,那么很难更新视图控制器。)



如果是这样,你能让我知道如何以漂亮的方式更新NSFetchedResultsController?



(更新)



参考NSfetchedResultsController,我读到以下字词:


因此,控制器有效地具有三种操作模式,通过它是否具有委托以及是否设置了高速缓存文件名来确定



无跟踪:代理设置为nil。控制器简单地
提供对执行获取时的数据的访问。



仅内存跟踪:委托是非零,文件缓存名
设置为nil。控制器监视其结果集中的对象,并且
响应相关的
更改来更新节和订单信息。



完全持续跟踪:代理和文件缓存名称是
非nil。控制器监视其结果集中的对象,并更新
部分和订购信息以响应相关更改。
控制器维护其
计算结果的持久高速缓存。


完全持续跟踪不是意味着我想要什么?我设置cacheName,但它工作原理相同。

解决方案


我认为NSFetchedResultController会监视持久
商店中的所有变化,希望?


FRC只会自动观察其抓取返回的对象。这通常不是问题,因为对FRC监视的对象的更改应该来自tableview UI或背景上下文。在前者中,FRC被警告该改变,并且在后者中,合并前景和背景上下文将触发FRC进行更新。



看起来你的代码更改了值,但是你没有通知FRC你做了任何更改。 (如果你有一个tableview显示 isMyTest == TRUE 的所有对象,那么显然你不能从 isMyTest == FALSE 。)在这种情况下,您需要注册tableview控制器:

  NSManagedObjectContextObjectsDidChangeNotification 

...从上下文的通知,以便您可以告诉FRC更新其观察之外的更改。


My program does work like link below:

Update results of NSFetchedResultsController without a new fetch

  1. show result of NSFetchedResultsController to UITableView
  2. get new object from web service and store it to core data (in same view controller, with RestKit)
  3. update table view with notification of NSFetchedResultsController delegate

The implementation of NSFetchedResultsControllerDelegate is copied from Apple's Core Data project and my predicated is:

[NSPredicate predicateWithFormat:@"isMyTest == TRUE"]

If the property update goes from TRUE to FALSE, it removes rows from the tableview (because the object for the row is in fetchedObjects of the NSFetchedResultsController)

However, if the property update goes from FALSE to TRUE, the NSFetchedResultsController notifies nothing, so the new data cannot be seen in table view. If I update BOTH NSFetchedResulsController and UITableView manually, it shows the new data.

I thought NSFetchedResultController watches all changes in persistent store, is it too big hope? :D

(I really want to do that because other view controller can update persistent store, then it is hard to update the view controller.)

If so, can you let me know how can I update NSFetchedResultsController in beautifully way?

(update)

in reference of NSfetchedResultsController, I read words below:

A controller thus effectively has three modes of operation, determined by whether it has a delegate and whether the cache file name is set.

No tracking: the delegate is set to nil. The controller simply provides access to the data as it was when the fetch was executed.

Memory-only tracking: the delegate is non-nil and the file cache name is set to nil. The controller monitors objects in its result set and updates section and ordering information in response to relevant changes.

Full persistent tracking: the delegate and the file cache name are non-nil. The controller monitors objects in its result set and updates section and ordering information in response to relevant changes. The controller maintains a persistent cache of the results of its computation.

"Full persistent tracking" does not mean what I want? I set up cacheName, but it works same.

解决方案

I thought NSFetchedResultController watches all changes in persistent store, is it too big hope?

The FRC only automatically observes the objects returned by its fetch. This is normally not a problem as changes to objects monitored by an FRC should arise from either the tableview UI or a background context. In the former, the FRC is alerted to the change and in the latter, merging the foreground and background context will trigger the FRC to update.

It sounds like you've got code changing values but you're not notifying the FRC that you've made any changes. (If you got a tableview displaying all objects whose isMyTest == TRUE then obviously you can't access objects from the UI whose isMyTest == FALSE.) In that case, you need to register the tableview controller for:

NSManagedObjectContextObjectsDidChangeNotification

… notifications from the context so that you can tell the FRC to update for changes made outside its observation.

这篇关于NSFetchedResultsController是否会观察对持久存储的所有更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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