1

我的 CoreData 结构是这样的:

A.setWithBs ->> B
B.setWithCs ->> C

现在,只知道 A,我需要获取 setWithBs 中唯一链接到特定 C 的 B。这可以通过单次获取优雅地完成吗?

谢谢

4

2 回答 2

0

您可以使用“self in”和“Subquery”请求进行尝试。没有测试过这种特殊情况,但这应该是你必须走的路

NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"B" inManagedObjectContext:context]];

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"self in %@ AND SUBQUERY(setWithCs, $sc, $sc == %@).@count > 0", A.setWithBs, C];
于 2013-10-21T10:38:20.717 回答
0

假设您还定义了反比关系(您应该这样做)

B.inA --> A, C.inB --> B

然后您可以执行以下获取请求:

A *theAObject = ...;
C *theCObject = ...;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"B"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(inA = %@) AND (ANY setWithCs = %@)",
         theAObject, theCObject];
[request setPredicate:predicate];

它获取与给定“A”对象相关(通过反向关系“inA”)以及与给定“C”对象相关(通过一对多关系“setWithCs”)的所有“B”对象。

于 2013-10-21T11:15:55.147 回答