使用NSFetchedResultsController在父对象中的关系中反映对象的更改 [英] Reflect changes to objects in a relationship in parent object with NSFetchedResultsController

查看:124
本文介绍了使用NSFetchedResultsController在父对象中的关系中反映对象的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体事件和时间。事件实体具有与时间实体的1对多关系,因为每个事件可以被执行多次。现在我想在tableView中按时间顺序显示所有事件。所以我设置了一个fetchedResultsController来获取所有的时间对象,根据开始时间排序,并通过使用事件对象的关系显示事件信息。到现在为止还挺好。但现在如果用户在表中选择一个条目,我将一个事件对象传递给可以编辑事件的detailViewController。

I have two entities event and time. The event entity has a 1 to many relationship to time entities as each event can be performed multiple times. Now I want to display all the events chronologically in a tableView. So I set up a fetchedResultsController to fetch all time objects, sort them according to the start time and display the event information by using the relationship to the event object. So far so good. But now if the user tabs an entry in the table I pass an event object to the detailViewController where the event can be edited.

问题是现在只有事件实体标记为已更新。我通过查看NSManagedObjectDidChange通知的userInfo目录找到了这一点。因此,FRC上的委托方法不会触发,因为没有更改时间对象。

The problem is that now only the event entity is marked as updated. I found this out by looking at the userInfo directory of the NSManagedObjectDidChange notification. In consequence the delegate methods on the FRC are not fired as no time objects have been changed.

如何手动将时间对象标记为已更改,以使FRC识别更改和更新单元格?我尝试了触发KVO方法 willChangeValueForKey didChangeValueForKey 但它没有工作到目前为止。

How can I manually mark a time object as changed to make the FRC recognize the changes and update the cells accordingly? I tried firing the KVO methods willChangeValueForKey and didChangeValueForKey but it did not work so far.

感谢alot
Thomas

Thanks alot Thomas

推荐答案

我的模型有点不同,

我有一个树形结构:

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


  • 元素

    • 标题

    • 父母一对


    • 子对象

    添加或删除文件时,只有队列中的第一个文件夹会收到有关此更改的通知。当文件的标题更改时,不会通知单个文件夹。那么,该怎么办?

    我尝试覆盖了-willChangeValueForKey:和-didChangeValueForKey:在我的Element类中。

    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天全站免登陆