关联计数更改时强制NSFetchedResultsController更新 [英] Force NSFetchedResultsController update when association count changes

查看:61
本文介绍了关联计数更改时强制NSFetchedResultsController更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSFetchedResultsController,它在表视图中显示项目列表,包括相关实体的计数.当为该关联添加一个对象(使用addXXXObject)时,不会调用任何回调来将更新通知我的控制器.

I have an NSFetchedResultsController which displays a list of items in a table view, including a count of an associated entity. When an object is added for this association (using addXXXObject), no callbacks are called to notify my controller of the update.

如何接收到对象添加到父实体的NSSet的通知,或者以其他方式强制获取的结果控制器进行更新?

How can I receive a notification of an object being added to the parent entity's NSSet, or otherwise force the fetched results controller to update?

要清楚,我当前正在使用parent.children.count检索计数,该计数可能不是最优的.有没有更好的方法来解决这整个问题?基本上,它只是一个类似于iPhone Mail应用程序的屏幕,其中的文件夹显示了其中的邮件数量.

To be clear, I'm currently retrieving the count using parent.children.count, which may be suboptimal. Is there a better way to go about this whole thing? It's basically just a screen like the iPhone Mail app, with folders showing a count of messages inside.

推荐答案

我的模型有些不同,但是可以轻松转换为您的模型.
我有一个树状结构:

My model is a little different, but it can easily be translated to your one.
I got a tree-like structure:

  • 元素
    • 标题
    • 父母(一对一)
    • 孩子(多对多)

    添加或删除文件时,只有队列中的第一个文件夹会收到有关此更改的通知.当文件标题更改时,不会通知单个文件夹.那么,该怎么办?
    我尝试在Element类中覆盖-willChangeValueForKey:和-didChangeValueForKey:

    When a file gets added or deleted, only the first folder in the queue up gets notified about this change. When a file's title changes, not a single folder would get notified. So, what to do?
    I tried overriding -willChangeValueForKey: and -didChangeValueForKey: in my Element class.

    - (void)willChangeValueForKey:(NSString *)key
    {
        [super willChangeValueForKey:key];
        [self.parent willChangeValueForKey:@"children"];
    }
    
    - (void)didChangeValueForKey:(NSString *)key
    {
        [super didChangeValueForKey:key];
        [self.parent didChangeValueForKey:@"children"];
    }
    

    基本上,由于子文件夹之一已更改,因此该操作强制父文件夹进行更新.
    希望它也对您有用.

    Basically, what this does is forcing the parent folder to update because one of its children changed.
    Hope it works for you, too.

    这篇关于关联计数更改时强制NSFetchedResultsController更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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