2014-01-22 31 views
1

阻止Restkit刪除孤立對象在我的應用程序中,我使用Restkit將JSON響應映射到核心數據。我使用 - addFetchRequestBlock - 這樣restkit將清理孤立的對象。如果服務器錯誤401

//清理孤立對象

[[RKObjectManager sharedManager] addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) { 

    // Value returned from the relativePath 
    RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"/filesystem/v1/folder/:folderId"]; 

    NSDictionary *argsDict = nil; 
    BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict]; 

    NSString *folderID; 

    if (match) 
    { 
     NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"ADStorageEntity"]; 

     // Get the folder ID 
     folderID = [argsDict objectForKey:@"folderId"]; 

     // Setup the fetchRequest 
     fetchRequest.predicate = [NSPredicate predicateWithFormat:@"parent = %@", @([folderID integerValue])]; // NOTE: Coerced from string to number 

     return fetchRequest; 
    } 

    return nil; 

}]; 

一切運作良好,但如果我有一個權威性錯誤401

File List Request Failed with Error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Loaded an unprocessable error response (401)

都在 「/:folderId」 的管理對象被視爲孤兒對象並被刪除。

有沒有辦法來防止這種行爲,而不是執行孤立的對象清理?

編輯 -

看來我是刪除錯誤映射後映射錯誤

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]]; 
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error.code" toKeyPath:@"code"]]; 
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error.message" toKeyPath:@"message"]]; 
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error.description" toKeyPath:@"description"]]; 

RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"error" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)]; 

[objectManager addResponseDescriptorsFromArray:@[errorDescriptor]]; 

,請將獲取請求塊不叫

+1

訪存請求塊僅用於成功的映射,如果401引發錯誤,則不應刪除任何內容。 401如何使用/處理您的響應描述符/配置? – Wain

+0

@Wain謝謝您的評論,它導致我發現問題(請參閱「編輯」)。 – Oren

回答

1

獲取請求塊僅在成功映射使用,如果401引發錯誤,則不應刪除任何內容 - 如果您沒有映射。

RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)涵蓋了4xx範圍內的所有代碼。你不需要使用它。如果您想忽略映射中的401響應,那麼您可以創建僅包含400(或400402,根據需要)的索引集。

相關問題