Restkit: How to define relationships for JSON object with dynamic keys?

116 views Asked by At

I have a JSON object with dynamic keys something like

carBrands:

{ 13950: { brand_id: "13950", name: "Toyota", country: "Japan", retailer: "" }, 13633: { brand_id: "13633", name: "NISSAN", country: "JAPAN", retailer: "" } }

This need to be mapped with a normal JSON object.

cars:

{ {id: "xyz" brand_id: "13950", status: "pending", user_id: "user1" }, {id: "ABC", brand_id: “13633”, status: "Delivered", user_id: "user2" } }

Here is the code for the carBrands object.

RKEntityMapping *carBrandsMapping = [RKEntityMapping mappingForEntityForName:@"CarBrands" inManagedObjectStore:managedObjectStore];    
carBrandsMapping.forceCollectionMapping = YES;    
[ carBrandsMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"brand_id"];    
carBrandsMapping.identificationAttributes = @[@"brand_id"];    
[ carBrandsMapping addAttributeMappingsFromDictionary:@{@"(brand_id).name": @"name",    
@"(brand_id).retailer": @"retailer",    
@"(brand_id).country":@"country"    
}];

RKResponseDescriptor *brandsRD = [RKResponseDescriptor responseDescriptorWithMapping:carBrandsMapping method:RKRequestMethodGET pathPattern:nil keyPath:@”brands” statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

For the cars object,

RKEntityMapping *carsMapping = [RKEntityMapping mappingForEntityForName:@"Cars" inManagedObjectStore:managedObjectStore];    
carsMapping.identificationAttributes = @[@"car_id"];   
[carsMapping addAttributeMappingsFromDictionary:@{@"id": @"car_id",    
@"brand_id": @"brand_id",    
@"status": @"status",    
@"user_id": @"user_id"    
}];

RKResponseDescriptor *carsRD = [RKResponseDescriptor responseDescriptorWithMapping:carsMapping method:RKRequestMethodGET pathPattern:nil keyPath:@”cars” statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

I tried the following statements for defining relationships.

[carsMapping addConnectionForRelationship:@"carBrands" connectedBy:@"brand_id"];

[carsMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"carBrands" toKeyPath:@"carsBrands" withMapping:brandsMapping]];

My problem is when I fetch the 'Car' data using following statements, my relationship object 'carBrands' is nil. I also got the 'found unmappable content' error. How to resolve it?

[[RKObjectManager sharedManager] getObjectsAtPath:PATH_FOR_CAR parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {    
self.carsArray = mappingResult.array;
NSLog(@"%@",mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Load failed with error: %@", error);
}];

Question:

How to define the relationship mapping, so that fetching 'Cars' entity will have the realtionship data 'carBrands' stored as faults and not nil?

@class CarBrands;    
@interface Cars: NSManagedObject    
@property (nonatomic, retain) NSString * car_id;    
@property (nonatomic, retain) NSString * brand_id;    
@property (nonatomic, retain) NSString * status;    
@property (nonatomic, retain) NSString * user_id;    
@property (nonatomic, retain) CarBrands *carBrands;    
@end

Thank you.

0

There are 0 answers