Ios 如何为多个实体创建一个fetchRequest?

Ios 如何为多个实体创建一个fetchRequest?,ios,Ios,以下问题的最新情况: 我们能够通过父子关系使用父实体获取请求查询子实体。示例查询如下: NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"fatherChild.name LIKE 'fx'"]; NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'f

以下问题的最新情况: 我们能够通过父子关系使用父实体获取请求查询子实体。示例查询如下:

  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"fatherChild.name LIKE 'fx'"];
  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];
现在我们要做的是使用上面的谓词,但另一个条件是,我们想找到一个给定父亲名字的孩子。我们使用了以下代码

  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];
但是程序崩溃了,只有一个例外: [\uu NSCFString CountByEnumerating with state:objects:count:]:发送到实例0xad49c40的无法识别的选择器

  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];
通过阅读示例,我们可以使用子查询,但不确定在我们的情况下,对于具有一对多关系的实体,语法是什么。如果有任何帮助,我们将不胜感激

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

  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];
问题: 我们有一个包含三个实体的数据模型:父亲、母亲和孩子。请参考图片

  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];
下面给出了我们对父实体的查询请求示例:

  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];
  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Father"
                                            inManagedObjectContext:managedObjectContext];
  [request setEntity:entity];

  NSString *attributeName = @"name";
  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"%K like %@",
     attributeName, searchField.text];
我们对母实体和子实体有类似的查询请求。我们想要做的是创建一个查询来组合父实体和母实体。例如,我们想在一个查询中搜索父亲的名字=迈克,母亲的名字=珍。我们怎么做

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

感谢您的回复。

我们发现可以使用子查询解决此问题

  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE %@ AND fatherMother.name LIKE %@ AND (0!=SUBQUERY(fatherChild,$varChild,$varChild.name=%@).@count)",dad.text, mom.text, baby.text];                                 
这里,
fatherMother
fatherChild
是关系的名称

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

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

我们发现可以使用子查询解决问题

  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];
NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE %@ AND fatherMother.name LIKE %@ AND (0!=SUBQUERY(fatherChild,$varChild,$varChild.name=%@).@count)",dad.text, mom.text, baby.text];                                 
这里,
fatherMother
fatherChild
是关系的名称

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

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

您是说您希望从一个查询中得到两种不同类型的结果,还是说您试图在
mother.name==foo
father.name==bar
中查找子项?后者是我们正在尝试的。非常感谢。你是说你想从一个查询中得到两种不同类型的结果,还是说你想在
mother.name==foo
father.name==bar
中查找子项?后者就是我们要做的。谢谢大家!@我这里也有类似的问题()。。。我尝试了以下谓词:[NSPredicate predicateWithFormat:@“mode=0和ALL(子查询(注册,$varEnrollment,$varEnrollment.mode=0))”],但它对我不起作用,在我的谓词中,注册是我的关系。。。你能告诉我哪里做错了吗?@yunas抱歉,我不知道,你应该问
用户1306828
,他是答案的作者。我只是帮他格式化答案中的代码。@tangqiaoboy我这里有类似的问题()。。。我尝试了以下谓词:[NSPredicate predicateWithFormat:@“mode=0和ALL(子查询(注册,$varEnrollment,$varEnrollment.mode=0))”],但它对我不起作用,在我的谓词中,注册是我的关系。。。你能告诉我哪里做错了吗?@yunas抱歉,我不知道,你应该问
用户1306828
,他是答案的作者。我只是帮他把答案中的代码格式化。
  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"name LIKE 'john' AND ANY fatherChild.name in 'fx'"];