2013-01-06 23 views
10

Tôi cần tạo hai loại POST khác nhau từ lớp Người dùng.Thêm hai bộ mô tả yêu cầu cho một lớp nhất định trong Restkit 0,2

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

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

Tôi đã cố gắng để thực hiện hai mô tả yêu cầu và thêm chúng vào quản lý đối tượng của tôi tuy nhiên tôi nhận được lỗi

"Không thể thêm một mô tả yêu cầu cho các lớp đối tượng giống như một yêu cầu hiện có bộ mô tả. "

Mã của tôi

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

Có ai biết làm thế nào tôi có thể giải quyết vấn đề này mà không cần tạo một lớp riêng biệt cho những yêu cầu này?

Mọi trợ giúp đều được đánh giá cao.

Cảm ơn.

Trả lời

5

Bạn có thể sử dụng ánh xạ động để chuyển đổi các hành vi tuần tự hóa. Nếu đây là vấn đề đủ phổ biến, chúng tôi có thể thêm con đường phù hợp với bộ mô tả yêu cầu. Tôi chỉ không có nhiều yêu cầu cho một tính năng như vậy.

Có một ví dụ về cách sử dụng các bản đồ năng động với một yêu cầu trong các bài kiểm tra đơn vị: https://github.com/RestKit/RestKit/blob/master/Tests/Logic/ObjectMapping/RKObjectParameterizationTest.m#L495-L534

+0

Cảm ơn các ví dụ Blake! Tôi hy vọng nó sẽ trở thành một vấn đề phổ biến! =) –

+0

Blake - Tôi đã gặp vấn đề tương tự gần đây và đã sử dụng hai cá thể ObjectManager (được cấp cho các đối tượng rất khác nhau). Đó có phải là phương pháp hay nhất được đề nghị không? – bobtheowl2

+7

"Nếu đây là vấn đề đủ phổ biến, chúng tôi có thể thêm con đường phù hợp với bộ mô tả yêu cầu" - nếu điều này có thể được coi là đề xuất tính năng, +1 cho nó :) – MrTJ

Các vấn đề liên quan