NSManagedObjectIDs数组,一次获取对象

时间:2011-11-20 21:32:22

标签: objective-c core-data nsmanagedobject nsmanagedobjectcontext

我有一个NSManagedObjectID数组。是否有更有效的方法来获取关联的托管对象,而不是循环遍历数组并单独获取它们?

1 个答案:

答案 0 :(得分:7)

使用以下谓词执行fetchRequest

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

完整示例

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;