核心数据 - 如何通过一个属性获取所有相关对象

时间:2013-08-23 14:12:25

标签: ios core-data relationship fetch one-to-one

如何通过一个属性获取相关对象并将其转储到一行中的nsarray中?

喜欢这个entity1attribute.enRelated.entity2attribute.

这是一对一的关系。

1 个答案:

答案 0 :(得分:2)

不要将它们转储到数组中。使用NSFetchedResultsController。然后,您将能够编写许多单行。

无论如何,代码逻辑是一样的。假设你已经在fetchedObjects中获取了所有对象,你可以这样做:

NSArray *filtered = [fetchedObjects filteredArrayUsingPredicate:
 [NSPredicate predicateWithFormat:@"otherEntity.attribute = %@", value]];

您可以使用托管对象子类上的类别缩短它,并将其简化为:

[myObject otherEntitiesWithAttribute:value];