Core Data / NSFetchedResultsController - Registering changed objects related to the fetched object

ⅰ亾dé卋堺 提交于 2020-01-24 19:43:05

问题


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?

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.

Thanks in advance!


回答1:


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

In 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"];
    }
}


来源:https://stackoverflow.com/questions/39792886/core-data-nsfetchedresultscontroller-registering-changed-objects-related-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!