我有一个NSManagedObjectID数组。是否有更有效的方法来获取关联的托管对象,而不是循环遍历数组并单独获取它们?
答案 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;