如何为多个实体创build一个fetchRequest?

更新下面的问题:我们能够通过使用fatherChild关系使用父实体获取请求来查询子实体。 示例查询如下所示:

NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"fatherChild.name LIKE 'fx'"]; 

现在我们要做的就是使用上面的谓词,而另一个条件是我们想为给定的父亲的名字find孩子。 我们使用了下面的代码

  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"]; 

但程序崩溃,例外:[__NSCFString countByEnumeratingWithState:objects:count:]:无法识别的select发送到实例0xad49c40

通过阅读示例,我们可以看到我们可以使用子查询,但不确定对于我们具有一对多关系的实体的情况,语法是什么。 任何帮助,将不胜感激?

谢谢。

问题:我们有一个三个实体的数据模型:父亲,母亲和孩子。 请参阅图像以供参考。

我们在父实体上的查询请求示例如下:

  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Father" inManagedObjectContext:managedObjectContext]; [request setEntity:entity]; NSString *attributeName = @"name"; NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"%K like %@", attributeName, searchField.text]; 

我们对母子实体也有类似的查询请求。 我们想要做的是创build一个查询来结合父亲和母亲的实体。 例如,我们想在一个查询中search父亲的名字= Mike和Mothers name = Jen。 我们该怎么做呢?

感谢您的答复。

我们发现我们可以使用SUBQUERY来解决这个问题。

 NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE %@ AND fatherMother.name LIKE %@ AND (0!=SUBQUERY(fatherChild,$varChild,$varChild.name=%@).@count)",dad.text, mom.text, baby.text]; 

这里fatherMotherfatherChild是关系的名字。

dad.text,mom.text和baby.text是UITextField值。