Array of NSManagedObjectIDs, fetch the objects at once
Asked Answered
D

1

6

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?

Dusa answered 20/11, 2011 at 21:32 Comment(0)
C
7

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;
Crossruff answered 20/11, 2011 at 21:38 Comment(1)
Thanks thats exactly what I was looking for. I didn't know you could use self as a managed object IDDusa

© 2022 - 2024 — McMap. All rights reserved.