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?
Array of NSManagedObjectIDs, fetch the objects at once
Asked Answered
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;
Thanks thats exactly what I was looking for. I didn't know you could use self as a managed object ID –
Dusa
© 2022 - 2024 — McMap. All rights reserved.