4
votes

I've looking for an answer for hours, I have no ideia what to do.

I have a JSON that I need to map, which has an array of string with no keyPath, but also has an attribute that needed to be mapped which has a keyPath. I was using RestKit 0.21.0, and had the same error when I updated to RestKit 0.22.0.

JSON:

{
  "content": {
    "site": "www.inf.ufsc.br/~fcdocil",
    "modulos": [
      "noticias",
      "fotos",
      "conteudo"
    ]
  }
}

RKResponseDescriptor:

RKEntityMapping *moduleMapping = [RKEntityMapping mappingForEntityForName:@"Module" inManagedObjectStore:store];
    [moduleMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"type"]];
    [moduleMapping addAttributeMappingsFromDictionary:@{@"@parent.site" : @"site"}];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:moduleMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"content.modulos" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

When I run this, it successful map the array in 3 different element, as you can see here:

SUCCESS (
    "<Module: 0x8e54280> (entity: Module; id: 0x8cd31f0 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p4> ; data: {\n    site = nil;\n    type = noticias;\n})",
    "<Module: 0x8e666b0> (entity: Module; id: 0x8cd3180 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p2> ; data: {\n    site = nil;\n    type = fotos;\n})",
    "<Module: 0x8e66a60> (entity: Module; id: 0x8cd3170 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p3> ; data: {\n    site = nil;\n    type = conteudo;\n})"
)

But It didn't do the @parent.site, which I desire is:

SUCCESS (
    "<Module: 0x8e54280> (entity: Module; id: 0x8cd31f0 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p4> ; data: {\n    site = www.inf.ufsc.br/~fcdocil;\n    type = noticias;\n})",
    "<Module: 0x8e666b0> (entity: Module; id: 0x8cd3180 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p2> ; data: {\n    site = www.inf.ufsc.br/~fcdocil;\n    type = fotos;\n})",
    "<Module: 0x8e66a60> (entity: Module; id: 0x8cd3170 <x-coredata://B31664A2-091E-43B8-9B36-EB7AF6C27292/Module/p3> ; data: {\n    site = www.inf.ufsc.br/~fcdocil;\n    type = conteudo;\n})"
)

I'm using CoreData to save the data, and my ManagedObject is like that,

Module.h

@interface Module : NSManagedObject

@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) NSString * site;

@end

Module.m

@implementation Module

@dynamic type;
@dynamic site;

@end

I really have no ideia what I'm doing wrong, so any suggestions would be appreciated. Thanks

1

1 Answers

1
votes

When you set the response descriptor key path to @"content.modulos" you are asking RestKit to throw away all of the higher and sibling information from the JSON (during the mapping process). So the @parent information doesn't exist.

You can't really map into your current desired structure. RestKit expects that you will map the site to one object and then map the modulos to another object (using a nested mapping connected with RKRelationshipMapping) and use a relationship to connect them. In this way you could use @parent, but you would no longer need to. Your single response descriptor would use the site mapping and a key path of @"content".