【问题标题】:iPhone Core Data get GrandchildreniPhone核心数据得到孙子
【发布时间】:2010-08-22 15:49:39
【问题描述】:

如果我为父 - 子 - 孙设置了实体以及它们之间的一对多关系,我知道我可以获取父的所有子实体或子的所有孙实体。

是否可以运行 fetch 以获取给定父级的所有 Granchild 实体?

感谢您的帮助!

【问题讨论】:

    标签: iphone core-data nsfetchrequest


    【解决方案1】:

    如果每个对多关系都没有反向关系,则不,您无法运行您描述的获取请求。但是,您应该对反向关系进行建模。 CoreData 使用逆来保持对象图的一致性。给定从ChildParentGrandchindChildparent 关系,您可以这样做

    Pareint *aParent; //already initialized
    
    NSSet *allGrandchildren = [aParent valueForKeyPath:@"children.@unionOfSets.children"];
    

    或对Grandchild 实体的获取请求,带有谓词:

    [NSPredicate predicateWithFormat:@"parent.parent == %@", aParent];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-04
      • 2012-03-28
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多