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

2024-01-07

我需要从 User 类发出两种不同类型的 POST。

//JSON Type A
{
    "password":"12345",
    "email":"[email protected] /cdn-cgi/l/email-protection"
}

//JSON Type B
{
   "user":{
      "Password":"12345",
      "Email":"[email protected] /cdn-cgi/l/email-protection"
   }
}

我尝试创建两个请求描述符并将它们添加到我的对象管理器中,但出现错误

“无法添加与同一对象类相同的请求描述符 现有的请求描述符。”

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

有谁知道我如何解决这个问题而不为这些请求创建单独的类?

任何帮助表示赞赏。

Thanks.


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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Restkit 0.2 中为给定类添加两个请求描述符 的相关文章

随机推荐