Adding two request descriptors for a given class i

2019-08-05 18:04发布

我需要两种不同类型的员额从用户类的到来。

//JSON Type A
{
    "password":"12345",
    "email":"test@gmail.com"
}

//JSON Type B
{
   "user":{
      "Password":"12345",
      "Email":"sample@gmail.com"
   }
}

我试图提出两个要求的描述,并将它们添加到我的对象管理器,但是我得到的错误

“不能对于相同的对象类的现有请求描述符添加请求描述符”。

我的代码

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

有谁知道我怎么能解决这个问题,而这些请求创建一个单独的类?

任何帮助表示赞赏。

谢谢。

Answer 1:

您可以使用动态映射切换序列化的行为。 如果这是一个普通不过的问题,我们可以想见,添加路径匹配请求描述符。 我只是还没有一吨这样的功能要求。

存在如何使用动态映射与该单元测试的请求的例子: https://github.com/RestKit/RestKit/blob/master/Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m#L495-L534



文章来源: Adding two request descriptors for a given class in Restkit 0.2
标签: ios restkit