【问题标题】:Array of NSManagedObjectIDs, fetch the objects at onceNSManagedObjectIDs 数组,一次获取对象
【发布时间】:2011-11-20 21:32:22
【问题描述】:

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

【问题讨论】:

    标签: objective-c core-data nsmanagedobject nsmanagedobjectcontext


    【解决方案1】:

    使用以下谓词执行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;
    

    【讨论】:

    • 谢谢,这正是我想要的。我不知道您可以使用 self 作为托管对象 ID
    猜你喜欢
    • 2021-11-01
    • 2018-06-07
    • 2016-12-22
    • 2022-01-23
    • 2021-08-07
    • 1970-01-01
    • 2021-12-07
    • 2022-01-10
    • 2020-02-07
    相关资源
    最近更新 更多