I have a need to be able to create the NSFetchedResultsController
dynamically, and am curious if this is even possible. The basis for this is that I need to be able to fetch results based on a variable. To my understanding I would need multiple NSFRCs to handle the multiple requests (it will be a UISegmentControl TableView
). The problem being I don't know how many controllers I will need as it is variable depending on what my API returns.
Is there a better method for this, could I not just pass a variable to NSFRC as the predicate?
Looking for best options to handle this.
An example would be:
I have 13 different status' for jobs (Open, On Hold, Cancelled, etc), fetching all jobs then running the filter would tremendously slow down the application returning ~ 50K records.
So my though was to fetch the status' use the status as a predicate and then fetch from the API jobs of that status.
What is my best approach?
You can modify the NSFetchRequest
of an NSFetchedResultsController
without problems. There is a special section in the documentation about this topic. If you use a cache, you need to delete it, afterwards you can change your fetch request and call performFetch:
again, on your NSFetchedResultsController
. The trick is to modify the existing NSFetchRequest
on your controller instead of trying to set a new fetch request, which will not work.
In your case, you might want to set the predicate of the fetch request by calling -[NSFetchRequest setPredicate:]
[NSFetchedResultsController deleteCacheWithName:cacheName];
[self.controller.fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"status == %@", currentStatus]];
NSError* error = nil;
if (![self.controller performFetch:&error]) {
NSLog(@"Error updating fetch: %@", error);
}
This should do what you want.