Core Data: Fetch via specific property (join relationship)

后端 未结 2 855
情话喂你
情话喂你 2021-02-10 16:41

I have an core data model as follows

\"alt

The attributes property of Page

2条回答
  •  迷失自我
    2021-02-10 16:50

    You should have a look at the predicate syntax for subqueries. You can not use the usual ANY keyword as this only allows you to match one column not two at the same time.

      NSString *keyValue = @"title";
      NSString *valueValue = @"home";
    
      NSFetchRequest *request = [[NSFetchRequest alloc] init];
      [request setEntity:[NSEntityDescription entityForName:@"Page" inManagedObjectContext:_context]];
      [request setPredicate:[NSPredicate predicateWithFormat:@"(SUBQUERY(attributes, $a, $a.key == %@ && $a.value == %@).@count != 0)", keyValue, valueValue]];
    

    The simpler predicate ANY attributes.key = "title" AND ANY attributes.value = "home" would not work as it also returns pages that have two dicts e.g. key='addr'/value='home' and key='title'/value='pete'.

提交回复
热议问题