I've got an array of 200 numbers (item ids) and I want to get some data from core data for each item using its item id. I am assuming the only way to do so is to query core data within a loop performing the fetchRequest in each iteration and adding the results to a mutable array. This seems like a memory hog and looping over 200 items may not be the best way of getting that data I need.
The destination for all this data is a table so I'd like to use an NSFetchedResultsController, however, that might be asking too much.
What is the best way to retrieve data from core data when you have several hundred items for which you wish to query?
Illustrative code examples would be most appreciated.
Here is my code:
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Shows" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"downloadCount" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"showId IN %@", resultsArray];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[sort release];
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}