Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we can't change FetchRequest at NSFetchedResultsController?

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?

like image 293
user4951 Avatar asked Aug 26 '11 06:08

user4951


People also ask

When should we use NSFetchedResultsController class?

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.

What is NSFetchedResultsController?

A controller that you use to manage the results of a Core Data fetch request and to display data to the user.


1 Answers

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.

like image 144
Cameron Spickert Avatar answered Nov 15 '22 20:11

Cameron Spickert