1

Update to the question below: We were able to query the child entity using the Father entity fetch request by using fatherChild relationship. Sample query is as follows:

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

Now what we are trying to do is to use the predicate above but another another condition where we want to find child for a given fathers name. We used the following code

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

But the program crashed with the exception: [__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xad49c40

Reading through examples we see we can use subquery but not sure what the syntax would be for our case where we have a entity with one to many relationship. Any help would be appreciated?

Thanks.

Question: We have a data model with three entities: Father, Mother and Child. Please see the image for reference.

An example Query request we have on the father entity is given below :

  NSEntityDescription *entity = [NSEntityDescription entityForName:@"Father"
                                            inManagedObjectContext:managedObjectContext];
  [request setEntity:entity];

  NSString *attributeName = @"name";
  NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"%K like %@",
     attributeName, searchField.text];

We have similar query request for Mother and Child entities. What we would like to do is to create one query to combine Father and Mother entities. For example we want to search on Father's name =Mike and Mothers name =Jen in a single query. How do we do it?

Thanks for your response.

user1306828
  • 715
  • 7
  • 14
  • 1
    Are you saying that you want two different types of results from a single query, or that you're trying to find children where `mother.name == foo` and `father.name == bar`? – Ian Henry Apr 01 '12 at 21:28
  • The latter is what we are trying to do. Thank you! – user1306828 Apr 01 '12 at 21:58

1 Answers1

1

We found out that we can solve the problem using a 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];                                 

Here fatherMother and fatherChild are names of relationships.

dad.text, mom.text and baby.text are UITextField values.

tangqiaoboy
  • 1,476
  • 1
  • 15
  • 32
user1306828
  • 715
  • 7
  • 14
  • @tangqiaoboy I have similar kind of question here(http://stackoverflow.com/questions/11934763/nspredicate-on-nested-object-nsset-to-filter-the-result-during-nsfetchrequest)... I have tried the following predicate : [NSPredicate predicateWithFormat:@"mode = 0 AND ALL ( SUBQUERY(enrollments,$varEnrollment,$varEnrollment.mode = 0))"] but it didn't work well for me, in my predicate enrollments is my relationship... can u suggest me where i am doing wrong ? – yunas Aug 15 '12 at 09:53
  • @yunas Sorry, I don't know, you should ask `user1306828` , he is the author of the answer. I just help him to format the code in the answer. – tangqiaoboy Aug 29 '12 at 04:15