christopher
christopher

Reputation: 520

RestKit: Mapping of primitive relationship

Hope the title describes it right.

I got the following JSON: {"regions":[{"name":"Region 1", "zipcodes":["80432", "80433"]}, ...]}

And I have two entities Region and ZipCode:

@interface Region : NSManagedObject
  @property (nonatomic, retain) NSString * name;
  @property (nonatomic, retain) NSSet *zipCodes;
@end

@interface ZipCode : NSManagedObject
  @property (nonatomic, retain) NSString * zipCode;
  @property (nonatomic, retain) Region *region;
@end

I try to fill both using the following code:

RKEntityMapping *regionMapping = [RKEntityMapping mappingForEntityForName:@"Region" inManagedObjectStore:managedObjectStore];

//[regionMapping addConnectionForRelationship:@"zipCodes" connectedBy:@{@"zipCode" : @"zipcodes"}];

[regionMapping addAttributeMappingsFromDictionary:@{@"name":@"name"}];

RKResponseDescriptor *regionDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:regionMapping pathPattern:@"/regions" keyPath:@"regions" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:regionDescriptor];

As you can see, I also tried to use addConnectionForRelationship: connectedBy: which I saw in this answer, but with no success. I guess I have to create a separate RKEntityMapping for ZipCode but I have no idea on how to glue that together with my existing code, without overloading the RKResponseDescriptor.

Does anyone of you have dealt with a similar situation? How did you solve it? The example from the link above is pretty similar, but unfortunately the solution posted is very incomplete.

Note 1: Existing code writes name into my database, but doesn't create any relationship with ZipCode nor does it write any zip code into my database

Note 2: I am using the latest RestKit Version (0.20pre)

Note 3: You could save my life here ;)

Many thanks in advance!

Upvotes: 1

Views: 412

Answers (1)

docksteaderluke
docksteaderluke

Reputation: 2255

I've done something similar and found that addRelationshipMappingWithSourceKeyPath:mapping: worked best. From my understanding, use relationship mappings for nested relationships (same request) and connections for creating relationships across multiple requests.

To do this you might want to reformat your JSON like so:

{"regions":[
    {"name":"Region 1", "zipcodes":[
        {"zipcode": "80432"},
        {"zipcode": "80433"}]},
    ...]}

...and change your mappings to the following:

RKEntityMapping *regionMapping = [RKEntityMapping mappingForEntityForName:@"Region" inManagedObjectStore:managedObjectStore];
[regionMapping addAttributeMappingsFromArray:@[@"name"]];

RKEntityMapping *zipCodeMapping = [RKEntityMapping mappingForEntityForName:@"ZipCode" inManagedObjectStore:managedObjectStore];
[zipCodeMapping addAttributeMappingsFromArray:@[@"name"]];

[regionMapping addRelationshipMappingWithSourceKeyPath:@"zipcodes" mapping:zipCodeMapping];

RKResponseDescriptor *regionDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:regionMapping pathPattern:@"/regions" keyPath:@"regions" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:regionDescriptor];

I haven't tried mapping a relationship with the JSON format you are using (array of strings rather than an array of dictionary elements) but you might be able to do it by playing around with the zipCode attribute mapping.

Upvotes: 2

Related Questions