Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of NSManagedObjectIDs, fetch the objects at once

I have an array of NSManagedObjectID. Is there a more efficient way to fetch the associated managed objects either than looping through the array and getting them individually?

like image 490
JPC Avatar asked Nov 20 '11 21:11

JPC


1 Answers

Perform a fetchRequest with the following predicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self in %@", arrayOfIds];

Full example

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = myEntityDescription;

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self in %@", arrayOfIds];

fetchRequest.predicate = predicate;
fetchRequest.sortDescriptors = mySortDescriptors;

NSError *error = nil;
NSArray *managedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release]; fetchRequest = nil;
like image 54
Paul.s Avatar answered Oct 28 '22 19:10

Paul.s