Confusion setting mapping in RKObjectMapping - Res

2019-07-04 19:02发布

问题:

Please help in set mapping with RestKit, I am lost to find how to do this.

My JSON is like

{
"result":{
    "success":"1",
    "message":"You logged in successfully.",
    "data":{
        "user_id":"1",
        "firstname":"somefirstname",
        "lastname":"somelastname",
        "username":"someusername",
        "country_id":"someid",
        "country_name":"somecountry",
        "phone":"1234567890",
        "status":"active"
        }
    }
}

What I have done so far is

RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[Login class]];
[loginMapping addAttributeMappingsFromDictionary:@{ @"user_id":@"intUserID", @"firstname":@"strFirstName", @"lastname":@"strLastName"}];        
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:@"PathTOLogin"
                                                                                          keyPath:@"result.data"
                                                                                      statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
NSURL *baseURL = [NSURL URLWithString:kAPIBaseURLString];
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
Login *loginObject = [[Login alloc] init];
[objectManager getObject:loginObject path:kUserLogin parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];

The problems is like some times JSON response may be like

{
"result":{
    "success":"0",
    "message":"Invalid Username or Password",      
    } 
}

So it throws error

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001

So I thought to add 2 more properties in Model class as success/message along with all other properties but I stuck on setting custom mapping as those properties are on root and other is in key path "result.data".

Can somebody please help.

回答1:

You should change your login mapping to something like:

RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[Login class]];
[loginMapping addAttributeMappingsFromDictionary:@{ @"success":@"success", @"message":@"message", @"data.user_id":@"intUserID", @"data.firstname":@"strFirstName", @"data.lastname":@"strLastName"}];        
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
                                                                                        method:RKRequestMethodGET
                                                                                   pathPattern:@"PathTOLogin"
                                                                                       keyPath:@"result"
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

Basically, remove 'data' from the key path of the response descriptor and instead use it in the mapping key paths.



回答2:

you can first check whether data exists or not by checking the following condition

if([yourDictionary valueForKeyPath:@"result.data"]){

   RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[Login class]];
[loginMapping addAttributeMappingsFromDictionary:@{ @"user_id":@"intUserID", @"firstname":@"strFirstName", @"lastname":@"strLastName"}];        
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
                                                                                            method:RKRequestMethodGET
                                                                                       pathPattern:@"PathTOLogin"
                                                                                          keyPath:@"result.data"
                                                                                      statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
NSURL *baseURL = [NSURL URLWithString:kAPIBaseURLString];
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
Login *loginObject = [[Login alloc] init];
[objectManager getObject:loginObject path:kUserLogin parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];

}
else{
    NSLog(@"No data available");
}

Hope this will help you.