Iphone 如何处理使用AFIncrementalStore时返回的部分关系数据?

Iphone 如何处理使用AFIncrementalStore时返回的部分关系数据?,iphone,ios,afnetworking,afincrementalstore,Iphone,Ios,Afnetworking,Afincrementalstore,正在试用一款IncrementalStore,它非常棒。然而,我遇到了一个情景,我不完全确定如何应对 我有一个模型,它返回子关系的id,但不返回内容本身。如: 获取/分组 {"id":3,"description":"My Group","category_id":2} 我可以通过常规get/categories获取所有类别。但我没有电话获取/group/3/categories 所以我不知道如何初始化group.category的子关系。我确信我遗漏了一些明显的东西,但任何帮助都将不胜感激

正在试用一款IncrementalStore,它非常棒。然而,我遇到了一个情景,我不完全确定如何应对

我有一个模型,它返回子关系的id,但不返回内容本身。如: 获取/分组

{"id":3,"description":"My Group","category_id":2}
我可以通过常规get/categories获取所有类别。但我没有电话获取/group/3/categories

所以我不知道如何初始化group.category的子关系。我确信我遗漏了一些明显的东西,但任何帮助都将不胜感激


有什么建议吗?

使用AFIS时,您存储的每个对象都有资源标识符。在大多数情况下,您希望对象的id是其RI。因此,在这种情况下,category_id是您的category对象的RI。因此,您只能使用category_id创建category对象。然后,当您获得所有category时,您将填充category对象的其他字段。它非常简单,因为AFIS足够智能


还有另一种方法。由于可以使用常规的get/categories获取所有类别,因此每个类别都应该有一个带有组id的字段。因此,您可以设置从类别到其组的关系。它将自动建立反向关系。

要基于@nataliq的答案,这必须在Afrest客户端的
表示关系实现中,从表示:of entity:fromResponse:
。它应该以关系名称(“category”)作为键,另一个NSDictionary以该类别的id作为值。如果您的组对象的类别关系命名为“Category”,并且您标准化了使用_id后缀命名另一个对象的id属性,则可以执行以下操作:

- (NSDictionary *)representationsForRelationshipsFromRepresentation:(NSDictionary *)representation
                                                           ofEntity:(NSEntityDescription *)entity
                                                       fromResponse:(NSHTTPURLResponse *)response
{
  NSMutableDictionary *mutableRelationshipRepresentations = [NSMutableDictionary dictionaryWithCapacity:[entity.relationshipsByName count]];
  [entity.relationshipsByName enumerateKeysAndObjectsUsingBlock:^(id name, id relationship, BOOL *stop) {
    id value = [representation valueForKey:[(NSString *)name stringByAppendingString:@"_id"]];
    if (value && ![value isKindOfClass:[NSNull class]] &&  ![relationship isToMany]) {
      NSDictionary *valueDict = @{@"id" : value};
      [mutableRelationshipRepresentations setValue:valueDict forKey:name];
    }
  }];

  return mutableRelationshipRepresentations;
}