Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put object mappings (in RestKIt)

As I don't want to hijack another thread here comes my question about mappings.

First read: Where's the best place to put object mappings in RestKit

I'm sure that the answer Blake Waters gave will probable be very correct as he is a much smarter and more experienced guy than I am, but to me logic tells me to put the mapping in each model: if you change something in your model, you're just a scroll away to change your mappings.

In my AppDelegate I would then just call the initMappings (or whatever you want to call it) in each of my models.

like image 224
Glenn Avatar asked Sep 13 '11 08:09

Glenn


1 Answers

I'm also a fan of placing the mappings with my models. I do it by adding a class method to each model so I can get the mapping whenever/wherever I need it.

+ (RKObjectMapping *)objectMapping 
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[self class]];

    [mapping mapKeyPath:@"Id"       toAttribute:@"id"];
    [mapping mapKeyPath:@"Subject"      toAttribute:@"subject"];
    [mapping mapKeyPath:@"Message"      toAttribute:@"message"];
    [mapping mapKeyPath:@"PostDate"     toAttribute:@"postDateStr"];
    [mapping mapKeyPath:@"StatusId"     toAttribute:@"statusId"];
    [mapping mapKeyPath:@"StatusDate"   toAttribute:@"statusDateStr"];

    mapping.setNilForMissingRelationships = YES;

    return mapping;
}
like image 90
Rob Booth Avatar answered Oct 14 '22 09:10

Rob Booth