如果服务器错误401,阻止Restkit删除孤立对象

时间:2014-01-22 13:37:10

标签: core-data restkit restkit-0.20 http-status-code-401

在我的应用程序中,我使用Restkit将JSON响应映射到核心数据。我使用 - addFetchRequestBlock - 这样restkit将清理孤立的对象。

//清除孤立对象

[[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;

}];

一切正常,但如果我有一个auth错误401

  

文件列表请求失败,错误:错误Domain = org.restkit.RestKit.ErrorDomain Code = -1011“加载了无法处理的错误响应(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 个答案:

答案 0 :(得分:1)

获取请求块仅用于成功映射,如果401引发错误,则不应删除任何内容 - 如果您没有映射。

RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)涵盖了4xx范围内的所有代码。你不需要使用它。如果要忽略映射中的401响应,则可以根据需要创建仅包含400(或400402等)的索引集。