Force update NSFetchedResultsController when changing the number of associations

I have an NSFetchedResultsController that displays a list of items in a table view, including a counter of a related object. When an object is added for this association (using addXXXObject), callers are not called to notify my controller of the update.

How can I get a notification about an object added to the parent NSSet, or otherwise make the updated result controller update?

To be clear, I'm currently getting an invoice using parent.children.count, which may be suboptimal. Is there a better way around this? It is basically just a screen, such as the iPhone Mail app, with folders displaying the number of messages inside.

+3
source share
1 answer

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

  • Element
    • title
    • parent (to-one)
  • Folder: Item
    • children (for many)
  • File: Element

When a file is added or deleted, only the first folder in the queue is notified of this change. When changing the file name, no folders will be notified. So what to do?
I tried to override -willChangeValueForKey: and -didChangeValueForKey: in the Element class.

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

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

, , , .
, .

0

Source: https://habr.com/ru/post/1767962/


All Articles