5

i am using coredata and restkit to map data.

here is the json response for getsales call

{   "success":true,
"sales" : [
            {
        "brands" :[
        {"id":"637", "name":"XYZ"},
        {"id":"638", "name":"abc"}
        ]
        "end_date" = "2011-10-14 12:00:00",
        "id" = 3794,
        "image" = "http://dummy.something.com.jpg",
        "name" = "test",
    },
       {
       "brands" =[
        {"id":"640", "name":"abc"}
        ],
        "end_date" = "2011-10-14 12:00:00",
        "id" = 3766,
        "image" = "http://dummy.something.com.jpg",
        "name" = "text2",
    },
       {
       "brands" =[
        {"id":"641", "name":"XYZ"},
        {"id":"642", "name":"abc"},
        {"id":"643", "name":"def"}
        ],
        "end_date" = "2011-11-06 12:00:00",
        "id" = 3798,
        "image" = "http://dummy.something.com.jpg",
        "name" = "test3",
    }
]
}

i have

@interface Sale : NSManagedObject{   
}
@property (nonatomic, retain) NSNumber * ID;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * endDate;
@property (nonatomic, retain) NSString * imageUrl;

@implementation Sale

@dynamic ID;
@dynamic name;
@dynamic startDate;
@dynamic endDate;
@dynamic imageUrl;

I am trying to map the response as following

- (void)getSales{
    RKObjectManager* objectManager = [RKObjectManager     objectManagerWithBaseURL:@"http://baseurl.com"];
    RKManagedObjectStore* objectStore = [RKManagedObjectStore     objectStoreWithStoreFilename:@"base.sqlite"];
objectManager.objectStore = objectStore;


RKManagedObjectMapping* saleMapping = [RKManagedObjectMapping mappingForClass:[Sale class]];
    [saleMapping mapKeyPathsToAttributes:
     @"id", @"ID",
     @"name", @"name",
     @"start_date",@"startDate",
     @"end_date", @"endDate",
     @"image", @"imageUrl",
     nil];

    saleMapping.primaryKeyAttribute = @"ID";
    [[RKObjectManager sharedManager].mappingProvider setMapping:saleMapping forKeyPath:@"sales"];

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/getSales/" objectMapping:saleMapping delegate:self];
}

essentially i have nested arrays of objects, what is the correct way to map these objects?? and what type of property should Sale class have to store the brands list??

Any help is appreciated, I already wasted too much time fixing this.

1 Answer 1

10

Try adding:

[saleMapping mapKeyPath:@"brands" toRelationship:@"brands" withMapping:[BrandObject objectMapping]];

where [BrandObject objectMapping] is the mapping for BrandObject (an NSManagedObject with id and name properties).

Essentially you can use mapKeyPath:toRelationship:withMapping: to nest mappings. You'll also need to add a property to your Sale object with the type NSSet:

@property (nonatomic, retain) NSSet *brands;

Finally, in the implementation of the Sale object, add: @dynamic brands alongside the other @dynamic statements.

Hope this helps! Thanks.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you (or anyone) explain the last step, "Add a dynamic property to your Sale object named NSSet *brands" in a little more detail? That's where I'm hung up.
I've updated with more detail about the NSSet property. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.