2013-04-28 42 views
0

我想问一下这里的东西。我已经映射了该对象并将其添加到RKResponseDescriptor中,它可以工作,但这里存在问题。当来自JSON的数据不同时,它不映射到已经创建的对象,我只是意识到它没有很好的映射。addResponseDescriptors不止一个

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

编辑号1:

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

为这一个,为用户的JSON数据可以被映射,但不为其他。我的意思是我们可以如何映射对象?因为我改变了responseDescriptors的顺序,它只是映射底部的对象。

对不起我的英文不好之前

+0

你是说当你第二次下载原始对象时没有得到更新? – Wain 2013-04-28 07:27:46

+0

我已经编辑了一些信息,我希望它有用 – maharprasetio 2013-04-28 08:40:16

回答

0

您的问题似乎是,你还没有设置任何描述符的pathPatternkeyPath。这意味着每当RestKit尝试处理任何JSON时,它都会尝试处理每个响应描述符,因为它们都始终匹配。所以你会得到一些好的对象和一些空的对象(创建但没有数据集)。

您需要教导响应描述符哪些数据适合映射。

如果您对每种类型的内容不同的URL:

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

设置pathPattern每个描述符:

userResponse:

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

如果您有不同的内容部分JSON然后你需要使用keyPath

添加尽可能多的信息到pathPatternkeyPath区分并允许RestKit了解您希望为每个请求接收的数据。