代码之家  ›  专栏  ›  技术社区  ›  Aran Mulholland JohnnyAce

核心数据:通过特定属性获取(连接关系)

  •  4
  • Aran Mulholland JohnnyAce  · 技术社区  · 15 年前

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

    alt text

    这个 attributes Page 是一套 DictionaryEntry ,它们是我的页面对象的值,非常类似于标准 NSDictionary (除所有 keys values 是字符串)

    页码 词典入口 具有 key="title" value="Home" . 如何形成一个fetch请求来加载那个特定的页面?

    2 回复  |  直到 15 年前
        1
  •  7
  •   Felix Lamouroux    15 年前

      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 = "title" AND ANY attributes.value = "home" 不起作用,因为它还返回有两个dict的页,例如key='addr'/value='home'和key='title'/value='pete'。

        2
  •  1
  •   Daniel D    15 年前

    假设你从知道字典的“键”开始,输入“标题”。

    创建NSPredicate时,为什么不使用已知的“键”在DictionaryEntry对象上创建NSFetchRequest呢。

        NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"DictionaryEntry" inManagedObjectContext:_context]];
    [request setPredicate:[NSPredicate predicateWithFormat:@"key == %@", keyValue]];