brainray
brainray

Reputation: 12884

one to many relationship not working (Restkit 0.20) addRelationshipMappingWithSourceKeyPath

I have a simpe one to many relationshipim my JSON and would like to map this into Objects (NOT Core Data entities). Mapping the poducts is working fine. But every product has an array of tracks that is not mapped.

This is the JSON:

enter image description here

Here is the code:

Product.h

#import <Foundation/Foundation.h>

@interface Product : NSObject

@property (nonatomic, strong) NSString* productid;
@property (nonatomic, strong) NSString* title;
@property (nonatomic, strong) NSString* artist;
//...
@property (nonatomic, strong) NSMutableArray *tracks;


Track.h

#import <Foundation/Foundation.h>

@interface Track : NSObject

@property (nonatomic, strong) NSString *artist;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *mixversion;



Here the code to request the JSON and map the response to Objects

/// Create Mappers for Product and Track
RKObjectMapping *mappingForProduct = [RKObjectMapping mappingForClass:[Product class]];

[mappingForProduct addAttributeMappingsFromArray:@[@"productid", @"title", @"tracks"]];


RKObjectMapping *mappingForTrack = [RKObjectMapping mappingForClass:[Track class]];
[mappingForTrack addAttributeMappingsFromArray:@[@"artist",@"title",@"mixversion"]];

/// assign relationship product-track
[mappingForProduct addRelationshipMappingWithSourceKeyPath:@"tracks" mapping:mappingForTrack];

/// setup responseDescriptor
NSString *key_path = @"products";
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mappingForProduct
                                                                                   pathPattern:nil
                                                                                       keyPath:key_path
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


/// Create Manager, connect responseDescriptor and execute
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"myRequestString"]];



[manager addResponseDescriptor:responseDescriptor];
[manager getObject:nil path:@"" parameters:nil
           success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
           {
               self.productsArray = [NSMutableArray arrayWithArray:mappingResult.array];
           }

           failure:^(RKObjectRequestOperation *operation, NSError *error)
           {
               NSLog(@"Error: %@", error);
           }


This gives an assertion error:

*** Assertion failure in -[RKObjectMapping addPropertyMapping:],     /Users/.../Pods/RestKit/Code/ObjectMapping/RKObjectMapping.m:235



The magic should happen in this line:

[mappingForProduct addRelationshipMappingWithSourceKeyPath:@"tracks" mapping:mappingForTrack];


... but for some reason it doesn't.

I've searched quite some sources on SO and the documentation, tried quite some combinations of keypath changed, but nothing will give the required result. A lots of the availble sample code is regarding older versions of RestKit or deals with Core Data. I just want to get mapped objects, no Core Data entities.

Thanks in advance for any hint.

Upvotes: 1

Views: 1470

Answers (1)

Wain
Wain

Reputation: 119031

You're just trying to add a mapping for tracks twice. It should be a relationship mapping, not an attribute mapping. Change the mappingForProduct to:

[mappingForProduct addAttributeMappingsFromArray:@[@"productid", @"title"]];

And the cause of the exception should be removed.

Upvotes: 1

Related Questions