核心数据/NSFetchedResultsController-注册与获取的对象相关的更改对象 [英] Core Data / NSFetchedResultsController - Registering changed objects related to the fetched object

查看:45
本文介绍了核心数据/NSFetchedResultsController-注册与获取的对象相关的更改对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NSFetchedResultsController 获取在 UITableView 中显示的对象列表.如果我更改对象中的值,则整个事件会触发并自动重新加载更改后的行.但是显示的值之一来自相关对象(一对多关系).这些对象具有临时标题(因此其值再次来自另一个对象).更改标题后,不会重新加载行.

I use a NSFetchedResultsController to get a list of objects which is displayed in a UITableView. If I change values in the objects the whole thing triggers and automatically reloads the changed rows. But one of the displayed values comes from related objects (one-to many relationship). Those objects have a transient title (so it's value comes again from another object). When this title changes the rows are not reloaded.

问题:有人可以为此建议一个干净的解决方案吗?

Question: Can anybody suggest a clean solution to this?

可能的肮脏解决方案:我可以在类中创建一个临时属性,并使用"fake" setter方法获取该属性,因此 NSFetchedResultsController 将看到更改并触发重新加载.但这在我看来是很肮脏的.

Possible Dirty Solution: I could create a transient property in the class which gets fetched with a "fake" setter method, so the NSFetchedResultsController will see a change and trigger a reload. But that's very dirty in my opinion.

提前谢谢!

推荐答案

FRC 跟踪一个特定实体的对象的属性变化.因此,不会跟踪相关实体的对象的更改.但是您可以使用KVO触发 FRC 反应.

FRC tracks changes in properties of objects of one particular entity. Changes in objects of related entity are therefore not tracked. But you can use KVO to trigger FRC reaction.

[Department].employees <->> [Employee].department

Employee.m 中:

- (void)setTitle:(NSString *)title
{
    [self willChangeValueForKey:@"title"];
    [self setPrimitiveValue:title forKey:@"title"];
    [self didChangeValueForKey:@"title"];

    [self.department willChangeValueForKey:@"employees"];
    [self.department didChangeValueForKey:@"employees"];
}

或者类似这样的东西(虽然我自己还没有测试过):

Or something like this (haven't tested it myself, though):

- (void)didChangeValueForKey:(NSString *)key
{
    [super didChangeValueForKey:key];

    if (self.department && [key isEqualToString:@"title"]) {
        [self.department willChangeValueForKey:@"employees"];
        [self.department didChangeValueForKey:@"employees"];
    }
}

这篇关于核心数据/NSFetchedResultsController-注册与获取的对象相关的更改对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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