核心数据:通过特定属性获取(连接关系) [英] Core Data: Fetch via specific property (join relationship)

查看:133
本文介绍了核心数据:通过特定属性获取(连接关系)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个核心数据模型如下

I have an core data model as follows

属性属性是一组 DictionaryEntry ,它们是我的Page对象的值,很像一个标准 NSDictionary (除了所有值是字符串)

The attributes property of Page is a set of DictionaryEntry, they are values for my Page objects, much like a standard NSDictionary (except all of the keys and values are strings)

键=title具有 DictionaryEntry value =Home

推荐答案

您应该查看子查询的谓词语法。您不能使用通常的ANY关键字,因为这只允许您匹配一列而不是两个同时。

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]];

更简单的谓词 ANY attributes.key =titleAND ANY attributes。 value =home将无法工作,因为它也会返回有两个例句的页面key ='addr'/ value ='home'a​​nd key ='title'/ value ='pete'。

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'.

这篇关于核心数据:通过特定属性获取(连接关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆