Search code examples
objective-cmappingrestkitdata-mapping

RestKit - Data Mapping


I'm new in RestKit. Can someone help me with mapping. I have JSON and I need to read and save data from JSON. How I can do it?

 {"data": {
    "viewer": {
      "themes": {
        "edges": [
          {
            "node": {
              "id": "61c39",
              "name": "ff"
            }
          }
        {
            "node": {
              "id": "dd95af4b-"",
              "name": "growth",
            }
          }
        ]
      }
    }
  }
}

Here is a part of my code for mapping:

// Defines mapping for DefaultThemes
RKEntityMapping *mappingDefaultTheme = [RKEntityMapping mappingForEntityForName:@"DefaultThemeData"
                                                         inManagedObjectStore:self.managedObjectStore];
mappingDefaultTheme.identificationAttributes = @[@"themeId"];
[mappingDefaultTheme addAttributeMappingsFromDictionary:@{ @"node.id" : @"themeId", @"node.name" : @"name"}];



RKResponseDescriptor *themeDefaultListResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mappingDefaultTheme
                                                                                                   method:RKRequestMethodGET
                                                                                              pathPattern:@"graphql"
                                                                                                  keyPath:@"edges.node"
                                                                                              statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

I have error when I running the app:

Error: No mappable object representations were found at the key paths searched.

So I need to know, how I should do data mapping?


Solution

  • Here is my solution:

    RKObjectMapping* chartDataDetailMapping = [RKObjectMapping mappingForClass:[MADefaultThemeData class]];
    
    [chartDataDetailMapping addAttributeMappingsFromDictionary:@{@"id": @"themeId",
                                                                 @"name": @"name"}];
    
    RKObjectMapping* chartDataMapping = [RKObjectMapping mappingForClass:[MANode class]];
    [chartDataMapping addAttributeMappingsFromDictionary:@{@"edges":@"edges"}];
    
    RKObjectMapping* alertInstanceMapping = [RKObjectMapping mappingForClass:[MADefaultThemeList class]];
    
    [alertInstanceMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"edges"
                                                                                         toKeyPath:@"edges"
                                                                                       withMapping:chartDataMapping]];
    
    [chartDataMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"node"
                                                                                         toKeyPath:@"themeData"
                                                                                       withMapping:chartDataDetailMapping]];
    
    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:alertInstanceMapping
                                                                                                            method:RKRequestMethodPOST
                                                                                                            pathPattern:@"graphql"
                                                                                                            keyPath:@"data.viewer.themes"
                                                                                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    

    [self addResponseDescriptor:responseDescriptor];