Example I have NSFetchedResultsController called at ListController called FetchController
+(NSFetchRequest * )fetchRequestInContext: (NSString*) entityName : (NSPredicate *) predicate : (NSString*) sortKey : (BOOL) sortAscending;
+(NSFetchedResultsController *) searchControllerInContext: (NSString*) entityName : (NSPredicate *) predicate : (NSString*) sortKey : (BOOL) sortAscending
{
NSFetchRequest *request = [self fetchRequestInContext:entityName :predicate :sortKey :sortAscending];
NSFetchedResultsController * FRC=[[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[ThreadClass managedObjectContext] sectionNameKeyPath:Nil cacheName:Nil]autorelease];
NSLog(@"FRC : %@",FRC);
return FRC;
}
look at that code, I call searchControllerInContext when I want to make NSFetchedResultsController and then perfom it with this code :
if (![[self ListController].FetchController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
the problem is I dont like to always performFetch when request is change, I want to make it easy with
[self ListController].FetchController.fetchRequest=[self FunctionTogetNewRequest];
but the [self ListController].FetchController.fetchRequest is readonly..
I want do this because I Don't want to perfomFetch again and again.. can I do that?
I mean, if I have a program that give records based on what user put in search box, should I create a new controller every time the search box content change? That would be strange. I thought the whole point of using NSFetchedResultsController is so we don't have to do that?
NSFetchedResultsController is a very useful class provided by the CoreData framework. It solves many performance issues you frequently run into while reading a large amount of data from database and displaying that data using a UITableview, UICollectionView or MKMapView.
A controller that you use to manage the results of a Core Data fetch request and to display data to the user.
Even though fetchRequest
is a readonly property, you can modify it. For example, setting the predicate of the fetch request works perfectly well as long as you're not caching the results (or as long as you delete the cache first). I've used this technique successfully in a number of projects. After modifying the fetch request, you should call performFetch
again.
Bottom line: you don't need to create a FRC every time the search terms change. Just delete your cache, change the fetch request's predicate and fetch the new result set with your existing instance.
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