Question

I wanna ask something in here. I've mapping the the object and add it to RKResponseDescriptor and it works, but there's a problem here. when the data from JSON is different, it doesn't mapping to the object that already created I just realize that it doesn't mapping well.

// Setup our object mappings
    RKObjectMapping *applicationMapping = [RKObjectMapping mappingForClass:[ApplicationSettings class]];
    [applicationMapping addAttributeMappingsFromDictionary:@{
     @"filterRead" : @"filterRead",
     @"filterUserComments" : @"filterUserComments"
     }];

    RKObjectMapping *categoryMapping = [RKObjectMapping mappingForClass:[Category class]];
    [categoryMapping addAttributeMappingsFromDictionary:@{
     @"id" : @"categoryID",
     @"title" : @"title",
     @"slug" : @"slug"
     }];

    RKObjectMapping *commentMapping = [RKObjectMapping mappingForClass:[Comment class]];
    commentMapping.forceCollectionMapping = YES;
    [commentMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"comments"];
    [categoryMapping addAttributeMappingsFromDictionary:@{
     @"(coments).id" : @"commentID",
     @"(comments).date" : @"createdDate",
     @"(comments).content" : @"comment"
     }];

    RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[Error class]];
    [errorMapping addAttributeMappingsFromDictionary:@{
     @"error" : @"error",
     @"status" : @"status"
     }];

    RKObjectMapping *postMapping = [RKObjectMapping mappingForClass:[Post class]];
    postMapping.forceCollectionMapping = YES;
    [postMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"posts"];
    [errorMapping addAttributeMappingsFromDictionary:@{
     @"id" : @"postID",
     @"title" : @"title",
     @"content" : @"content",
     @"date" : @"createdDate",
     @"modified" : @"modifiedDate"
     }];

    RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
    [userMapping addAttributeMappingsFromDictionary:@{
     @"nonce" : @"nonce",
     @"cookie" : @"cookie",
     @"username" : @"userName",
     @"email" : @"email"
     }];

    // Register our mappings with the provider using a response descriptor
    RKResponseDescriptor *userResponse = [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                                                 pathPattern:nil
                                                                                     keyPath:nil
                                                                                 statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *applicationResponse = [RKResponseDescriptor responseDescriptorWithMapping:applicationMapping
                                                                                       pathPattern:nil
                                                                                           keyPath:nil
                                                                                       statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *categoryResponse = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping
                                                                                        pathPattern:nil
                                                                                            keyPath:nil
                                                                                        statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *commentResponse = [RKResponseDescriptor responseDescriptorWithMapping:commentMapping
                                                                                        pathPattern:nil
                                                                                            keyPath:nil
                                                                                        statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *errorResponse = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
                                                                                    pathPattern:nil
                                                                                        keyPath:nil
                                                                                    statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    RKResponseDescriptor *postResponse = [RKResponseDescriptor responseDescriptorWithMapping:postMapping
                                                                                    pathPattern:nil
                                                                                        keyPath:nil
                                                                                    statusCodes:(RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful))];

    [objectManager addResponseDescriptorsFromArray:
     @[
        applicationResponse,
        categoryResponse,
        commentResponse,
        userResponse,
        postResponse,
        errorResponse
     ]] ;

edit number 1:

    [objectManager addResponseDescriptorsFromArray:
     @[
        applicationResponse,
        categoryResponse,
        commentResponse,
        errorResponse,
        postResponse,
        userResponse
     ]] ;

for this one, the JSON data for user can be mapped but not for the other. What I mean is can we mapped the object something like this? because I was changed the order of the responseDescriptors and it just mapped the object at the bottom.

Sorry for my bad english before

Was it helpful?

Solution

Your issue would seem to be that you haven't set the pathPattern or keyPath of any of the descriptors. This means that whenever RestKit tries to process any JSON it will try to process every single response descriptor as they all always match. So you'll get some good objects, and some empty objects (created but no data set).

You need to teach the response descriptors which data they are suitable for mapping.

If you have different URLs for each type of content:

##_URL_##/users.json
##_URL_##/categories.json

set the pathPattern for each descriptor:

userResponse:

...ithMapping:userMapping
  pathPattern:@"users.json"
      keyPath:nil ...

If you have different sections of content in the JSON then you need to use the keyPath.

Add as much information as you can to the pathPattern and keyPath to differentiate and allow RestKit to make sense of the data you expect to receive for each request you make.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top