Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop through array & fetch data from Core-Data
    primarykey
    data
    text
    <p>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.</p> <p>The destination for all this data is a table so I'd like to use an NSFetchedResultsController, however, that might be asking too much.</p> <p>What is the best way to retrieve data from core data when you have several hundred items for which you wish to query?</p> <p>Illustrative code examples would be most appreciated. </p> <p>Here is my code:</p> <pre><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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload