开发者

Force NSFetchedResultsController update when association count changes

开发者 https://www.devze.com 2023-01-18 13:44 出处:网络
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 ca

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.

How can I receive a notification of an object being added to the parent entity's NSSet, or otherwis开发者_如何学Pythone force the fetched results controller to update?

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
    • title
    • parent (to-one)
  • Folder : Element
    • children (to-many)
  • File : 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号