Question

Hi stackoverflow community, i'm using RKObjectManager to make iOS RESTful requests to map responses to local objects.

My problem with Restkit, is to register multiple classes for the same Element in different requests. In this requests i have nested objects with the same element name "List".

Here is part of the code:

// Get array of RKObjects1 - First request of the queue
RKObjectManager * objectManager = [RKObjectManager sharedManager];
[objectManager registerClass:[RKTObject1 class] forElementNamed:@"List"];

RKObjectLoader * loader = [objectManager objectLoaderWithResourcePath:@"objects1" delegate:self];
[loader setObjectClass:[RKTList class]];



// Get array of RKObjects2 - Second request of the queue
RKObjectManager * objectManager = [RKObjectManager sharedManager];
[objectManager registerClass:[RKObjects2 class] forElementNamed:@"List"];

RKObjectLoader * loader = [objectManager objectLoaderWithResourcePath:@"objects2" delegate:self];
[loader setObjectClass:[RKTList class]];

What i would like to know, if there is any solution to register multiple classes for the same element in different requests?

Was it helpful?

Solution

Well, to solve my problem, i update restkit and with the new Object Mapping 2.0 it's possible to register multiple classes for the same element keys.

You only need to create different RKObjectMapping objects for each request and make setObjectMapping of each object in RKObjectLoader.

For example:

RKObjectMappingProvider * mappingProvider = [RKObjectManager sharedManager].mappingProvider; 

// Create 2 RKObjectMapping for each request
RKObjectMapping * object1Mapping = [RKObjectMapping mappingForClass:[RKTList class]];
[object1Mapping mapKeyPath:@"Id" toAttribute:@"idObject"];
[object1Mapping mapKeyPath:@"Name" toAttribute:@"name"];
[mappingProvider setMapping:object1Mapping forKeyPath:@"Objects1List"];


RKObjectMapping * object2Mapping = [RKObjectMapping mappingForClass:[RKTList class]];
[object2Mapping mapKeyPath:@"Id" toAttribute:@"idObject"];
[object2Mapping mapKeyPath:@"Name" toAttribute:@"name"];
[mappingProvider setMapping:object2Mapping forKeyPath:@"Objects2List"];

//Make the 2 request with explicit object mapping
RKObjectManager * objectManager = [RKObjectManager sharedManager];
RKObjectLoader * loaderFirstRequest = [objectManager objectLoaderWithResourcePath:@"objects1" delegate:self];
[loaderFirstRequest setObjectMapping:object1Mapping];

RKObjectLoader * loaderSecondRequest = [objectManager objectLoaderWithResourcePath:@"objects2" delegate:self];
[loaderSecondRequest setObjectMapping:object2Mapping];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top