aryaxt
aryaxt

Reputation: 77596

iphone Core Data - Filtering NSFetchedResultController?

I was given a framework written by other programmers to access the core data. In this situation i receive a pre-loaded NSFetchedResultController which I need to filter, to display a part of it's data.

Here is what I tried:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category==%@", @"current"];
[NSFetchedResultsController deleteCacheWithName:@"Root"];
[myResultController.fetchRequest setPredicate:predicate];
myResultController.fetchedObjects = [myResultController.fetchedObjects filteredArrayUsingPredicate:predicate];

And i get an error saying that object cannot be set, either setter method is missing, or object is read-only.

So whats the best way to filter an NSFetchResultController which is already loaded, without having to store the filtered data into another array?

Upvotes: 0

Views: 812

Answers (1)

vfn
vfn

Reputation: 6066

fetchedObjects is readonly. You cannot set it manualy.

What you need to do is to perform a new fetch with your myResultController.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category==%@", @"current"];
[NSFetchedResultsController deleteCacheWithName:@"Root"];
[myResultController.fetchRequest setPredicate:predicate];
//myResultController.fetchedObjects = [myResultController.fetchedObjects filteredArrayUsingPredicate:predicate];
[myResultController performFetch:nil];

Upvotes: 5

Related Questions