There are a lot of questions on this topic but perhaps all too specific and none too concise.
I have a NSFetchedResultsController. It initially fetches the data I need. When I update the data model which would affect the results of the NSPredicate of the NSFetchRequest, the content does not update.
More concretely, I have a Permissions model. There are data objects that are assigned permissions, then there are users who have a subset of these permissions, though the data object's Permission is not the same as the User's permission; they do share the same controlKey.
So, the NSPredicate is:
[NSPredicate predicateWithFormat:@"controlKey IN %@", [[User currentUser].permissions valueForKey:@"controlKey"]];
The problem is, if I create and add a permission, the content does not update when I save the NSManagedObjectContext (yes, I'm observing it. No, I'm not using a cache so nothing to delete).
I'm pretty convinced it's because of the predicate. It's still the same array as initially, and doesn't get updated.
My question is, how do I write a predicate that gets me what I want but still remains "dynamic" ? It would be nice to have UITableView animations as this object is added.
Here is what you need to do. When detecting the change,
self.fetchedResultsController.predicate = //new predicate
[self.fetchedResultsController performFetch:nil];
[self.tableView reloadData];
Or sometimes it is necessary to wipe the FRC clean.
_predicate = // new predicate, put it into an ivar
self.fetchedResultsController = nil;
[self.tableView reloadData]; // lazily re-instantiate FRC with _predicate
This could be expensive, but in order to get animations, you could try replacing reloadData with the following:
[self.tableView beginUpdates];
[self.tableView endUpdates];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With