Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResKit iOS - Register multiple classes for same Element

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?

like image 365
Damien Vieira Avatar asked Jun 13 '11 16:06

Damien Vieira


1 Answers

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];
like image 195
Damien Vieira Avatar answered Oct 14 '22 15:10

Damien Vieira