4

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

Wain
  • 118,658
  • 15
  • 128
  • 151
Felipe Docil
  • 587
  • 6
  • 11

1 Answers1

1

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".

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thank you, I didn't want use relationship because this data is going to fill a TableView, so it's much easier when you fetch the table `Module` and then use a predicate to select the `site` which you desire. Well but if it's the RestKit design I only have to follow. And I won't choose your answer since wasn't what I was looking for, although it was helpful. Thank you again. – Felipe Docil Nov 25 '13 at 10:20
  • Predicates can traverse relationships so you can do your table and filter. The only thing to keep in mind is that if you use an FRC then it won't update for changes in the relationships content. – Wain Nov 25 '13 at 11:17