在Restkit 0.2中为给定类添加两个请求描述符

问题描述 投票:11回答:2

我需要从User类进行两种不同类型的POST。

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

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

我尝试制作两个请求描述符并将它们添加到我的对象管理器中,但是却收到错误消息

“无法为与现有的请求描述符。“

我的代码

@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];
}

有人知道如何解决这些问题而无需为这些请求创建单独的类吗?

感谢您的帮助。

谢谢。

ios restkit
2个回答
5
投票

您可以使用动态映射来切换序列化行为。如果这是一个足够普遍的问题,我们可以想象将路径匹配添加到请求描述符。我只是没有大量要求使用此功能。

在单元测试中,有一个示例如何将动态映射与请求一起使用:https://github.com/RestKit/RestKit/blob/master/Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m#L495-L534


0
投票

对于多个请求描述符,我声明了一个新的模型类,该模型类具有与较早的模型相同的数据成员,然后在添加请求描述符而不是如下的较早的模型时对其进行引用。

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

这里新创建的类是“ CMAGoogleUserDataModel”

注:我不确定是否是经过优化的,但是确实解决了我的用例。

© www.soinside.com 2019 - 2024. All rights reserved.