Frank Fu
Frank Fu

Reputation: 3643

Adding two request descriptors for a given class in Restkit 0.2

I need to make two different types of POST coming from the User class.

//JSON Type A
{
    "password":"12345",
    "email":"[email protected]"
}

//JSON Type B
{
   "user":{
      "Password":"12345",
      "Email":"[email protected]"
   }
}

I've tried to make two request descriptors and adding them to my object manager however I get the error

"Cannot add a request descriptor for the same object class as an existing request descriptor."

My code

@interface User : NSObject

@property (nonatomic, retain) NSString * userID;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSString * firstName;
@property (nonatomic, retain) NSString * lastName;

@end

- (void)setupUserMapping:(RKObjectManager *)objectManager {

    // Setup user response mappings
    RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
    [userMapping addAttributeMappingsFromDictionary:@{
     @"ID" :@"userID",
     @"Email" : @"email",
     @"Password" : @"password",
     @"FirstName" : @"firstName",
     @"LastName" : @"lastName",
     }];


    RKResponseDescriptor *responseDescriptorAuthenticate = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                       pathPattern:@"/Authenticate"
                                                                                           keyPath:nil
                                                                                       statusCodes:[NSIndexSet indexSetWithIndex:200]];


    RKResponseDescriptor *responseDescriptorRegister = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                                   pathPattern:@"/Register"
                                                                                                       keyPath:nil
                                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:200]];
    [objectManager addResponseDescriptor:responseDescriptorRegister];
    [objectManager addResponseDescriptor:responseDescriptorAuthenticate];

    // Setup user request mappings
    RKObjectMapping* userRequestMappingForRegister = [RKObjectMapping requestMapping];
    [userRequestMappingForRegister addAttributeMappingsFromDictionary:@{
     @"email" : @"Email",
     @"password" : @"Password",
     @"firstName" : @"FirstName",
     @"lastName" : @"LastName",
     }];
    RKRequestDescriptor *requestDescriptorForRegister = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMappingForRegister objectClass:[User class] rootKeyPath:@"user"];


    RKObjectMapping* userRequestMappingForAuthenticate = [RKObjectMapping requestMapping];
    [userRequestMappingForAuthenticate addAttributeMappingsFromDictionary:@{
     @"userID" :@"ID",
     @"email" : @"email",
     @"password": @"password"
     }];
    RKRequestDescriptor *requestDescriptorForAuthenticate = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMappingForAuthenticate objectClass:[User class] rootKeyPath:nil];

    [objectManager addRequestDescriptor:requestDescriptorForRegister];
    [objectManager addRequestDescriptor:requestDescriptorForAuthenticate];
}

Does anyone know how I can solve this problem without creating a separate class for these requests?

Any help is appreciated.

Thanks.

Upvotes: 11

Views: 4069

Answers (2)

Muhammad Bilal ahmad
Muhammad Bilal ahmad

Reputation: 325

For multiple request descriptors, I declared a new model class with the same data members as the earlier one, and then referenced it while adding the request descriptor instead of the earlier one as follow.

    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[CMAGoogleUserDataModel class]];

Here the newly created class was "CMAGoogleUserDataModel"

Noted: I am not sure whether it is the optimised one or not, but it did solve my use case.

Upvotes: 0

Blake Watters
Blake Watters

Reputation: 6617

You can use a dynamic mapping to switch the serialization behaviors. If this is a common enough issue, we could conceivably add path matching to the request descriptor. I just have not had a ton of requests for such a feature.

There is an example of how to use the dynamic mapping with a request in the unit tests: https://github.com/RestKit/RestKit/blob/master/Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m#L495-L534

Upvotes: 5

Related Questions