I have an array of 200 numbers (item identifiers) and I want to get some data from the master data for each element using its element identifier. I assume that the only way to do this is to query the main data in a loop that executes fetchRequest in each iteration, and add the results to the mutable array. It looks like a burst of memory and a cycle of more than 200 elements, may not be the best way to get the data I need.
The assignment for all this data is a table, so I would like to use NSFetchedResultsController, however this may require too much.
What is the best way to get data from master data if you have several hundred items that you want to query for?
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;
}
source
share