2013-04-28 35 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瞭解您希望爲每個請求接收的數據。