2014-03-12 12 views
0

我正在寻找将我所有的Foursquare列表都存入Core Data。我想使用Restkit来实现这一点。在/ V2 /用户/自我的结构/列出的回应是:在Foursquare的列表API中使用RKDynamicMapping捕获列表

"response": { 
    "lists": { 
     "count": 8, 
     "groups": [ 
      { 
       "type": "created", 
       "name": "Lists You've Created", 
       "count": 6, 
       "items": [ 
        { 
         "id": "13250/todos", 
         "name": "My to-do list", ... 
         } 
        { 
         "id": "13251/something", 
         "name": "Some List", ... 
        }, 
        { 
         "id": "13252/somethingelse", 
         "name": "Some Other List", ... 
        } 
       ] 
      }, 
      { 
       "type": "followed", 
       "name": "Lists You've Saved", 
       "count": 1, 
       "items": [ 
        { 
         "id": "5105e3cae4b0e721ca7b400a", 
         "name": "Portland's Best Coffee - 2012", ... 
         } 
        { 
         ... 
        } 
       ] 
      } 
     ] 
    } 

正如你可以看到有下的keyPath response.lists.groups 2名列表。最终,我想将这两个列表合并为1,但我很乐意收到2个单独的列表。

我建立了我的映射如下:

RKEntityMapping* listMapping = [RKEntityMapping mappingForEntityForName:[FOFSList entityName] 
                inManagedObjectStore:objectManager.managedObjectStore]; 
[listMapping addAttributeMappingsFromDictionary:@{ 
                @"id": @"listID", 
                @"title": @"name", 
                @"description": @"desc", 
                @"user": @"user", 
                @"following": @"following", 
                @"collaborative": @"collaborative", 
                @"canonicalUrl": @"canonicalUrl", 
                @"venueCount": @"venueCount", 
                @"visitedCount": @"visitedCount" 
                }]; 

RKDynamicMapping *dynamicMapping = [RKDynamicMapping new]; 
[listMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil 
                      toKeyPath:@"items" 
                      withMapping:dynamicMapping]]; 

RKResponseDescriptor *listResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:listMapping 
                         method:RKRequestMethodGET 
                         pathPattern:nil 
                          keyPath:@"response.lists.groups" 
                         statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
[objectManager addResponseDescriptor:listResponseDescriptor]; 
[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) { 
    if ([[representation valueForKey:@"type"] isEqualToString:@"created"]) { 
     return listMapping; 
    } else if ([[representation valueForKey:@"type"] isEqualToString:@"followed"]) { 
     return listMapping; 
    } 

    return nil; 
}]; 

listMapping.identificationAttributes = @[ @"listID" ]; 

我结束了一个错误:

* Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key propertyMappings.'

我应该使用RKDynamicMappings?是否有一些技巧可以解析这种样式的响应?

回答

1

对于那些有兴趣,我有一点点的创意与RKResponseDescriptor

RKResponseDescriptor *listResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:listMapping 
                          method:RKRequestMethodGET 
                         pathPattern:nil 
                          keyPath:@"[email protected]" 
                         statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

见集合操作@distinctUinionOfArrays是什么最终让我我需要的东西。它使2组数组联合,然后从数组联合中的每个对象中抓取项键。

0

此刻动态映射作为类型的过滤器。这很好,如果它是你想要的,并且是实现过滤器的正确方法。但是,您正在以错误的方式应用该映射。

动态映射应作为响应描述符的映射。它分析传入的对象并返回适用的映射。

您需要一个新的非动态映射来处理嵌套的items

关于合并的其他问题不能在映射过程中处理,但可以通过向目标类中添加一个方法来完成,该方法与映射数组一起调用,并与现有数组合并并推入合并结果进入真实的实例变量。映射目标将是方法而不是实例变量。

+0

您提到:“您需要一个新的非动态映射来处理嵌套的项目。”这就是'listMapping'的作用。它在那里映射动态映射找到的各个对象。我误解你在说什么吗? –

相关问题