Loop and fetch data from Core-Data

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;    

}
+3
source share
1 answer

It is best to use a predicate. For instance:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// Set the entity for the fetch request.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"myID IN %@", IDArray];

// Set the predicate for the fetch request.
[fetchRequest setPredicate:predicate];

// Perform the fetch.
NSError *error;
NSArray *array = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

This is a rough example, because I don’t know the form of your identifiers or what is in the main data, but basically you can search for the main data only for the identifiers stored in your array, and further implement the predicate for if you want, return only certain values . That should at least get you started.

+5
source

All Articles