Where is the best place to place object mappings in RestKit

I use RestKit for the project, and I noticed that in this class there is no longer a method that can be controlled by all the mappings in (elementToPropertyMappings), so I wondered where it is best to place the new code, I am currently doing this in my view controller , but I will use most of the same comparisons in other areas of my code, so there is a more efficient place to put it:

The code that I mean is:

RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:[User class]]; [userMapping mapKeyPath:@"id" toAttribute:@"identifier"]; [userMapping mapKeyPath:@"forename" toAttribute:@"forename"]; [userMapping mapKeyPath:@"surname" toAttribute:@"surname"]; [userMapping mapKeyPath:@"email" toAttribute:@"email"]; [userMapping mapKeyPath:@"twitter_username" toAttribute:@"twitterUsername"]; [userMapping mapKeyPath:@"created" toAttribute:@"created"]; [userMapping mapKeyPath:@"use_gravatar" toAttribute:@"useGravatar"]; [userMapping mapKeyPath:@"avatar_url" toAttribute:@"avatarURL"]; [userMapping mapKeyPath:@"gender" toAttribute:@"gender"]; [[RKObjectManager sharedManager].mappingProvider setMapping:userMapping forKeyPath:@"user"]; 

It would be great if it could be in the method of the User class, which I can then call to configure these mappings, etc.

Many thanks

+8
object mapping objective-c restkit
source share
3 answers

There are several good ways to streamline your mappings currently recommended:

  • Put all your mappings in the application delegate and configure them when you initialize the application. This prevents them from being controlled and is useful if you have a small number of mappings.
  • Subclass RKObjectMappingProvider and build all your mappings in the init method of your subclass, then instantiate and assign an instance of the mapping provider to the object manager.
  • Add a category to RKObjectMappingProvider using a method of type defineMappings. Then import the header into your application delegate and call it via [mappingProvider defineMappings] after RestKit is initialized.

The problem with defining mappings in a class method is that when you have relationships, you can complete circular dependencies, since there are no ivars available to store instances. We can do something with blocks to help solve this problem, but such work has not yet been completed.

+9
source share

I think the domain model should be the only one who needs to know about mapping. Here is how I map some flickr json:

I call mappings when you need them (maybe in AppDelegate or anywhere)

 // Setup our object mappings RKObjectMapping *photoMapping = [Photo mapWithMapping:nil]; RKObjectMapping *photosMapping = [Photos mapWithMapping:photoMapping]; 

And this is one of my domain objects: It contains the Method class that performs the mapping.

 #import "Photos.h" @implementation Photos + (RKObjectMapping*)mapWithMapping: (RKObjectMapping*)aMapping { RKObjectManager *objectManager = [RKObjectManager sharedManager]; RKManagedObjectMapping *mapping = [RKManagedObjectMapping mappingForClass:[self class] inManagedObjectStore:objectManager.objectStore]; [mapping mapKeyPathsToAttributes: @"page", @"page", @"pages", @"pages", @"perpage", @"perpage", @"stat", @"stat", @"total", @"total", nil]; if (aMapping) { [mapping mapRelationship:@"photo" withMapping:aMapping]; } return mapping; } @end 
+3
source share

Usually I create a private method in my applicationโ€™s division, which I call during the installation of the application that configures RKObjectManager, and then creates all the mappings that I use throughout the application. I believe RestKit sample projects use the same template, so definitely check them out as a link.

+1
source share

All Articles