Core data 如果服务器出现错误,请防止Restkit删除孤立对象
在我的应用程序中,我使用Restkit将JSON响应映射到核心数据。我使用-,这样restkit将清理孤立对象 //孤立对象的清理Core data 如果服务器出现错误,请防止Restkit删除孤立对象,core-data,restkit,restkit-0.20,http-status-code-401,Core Data,Restkit,Restkit 0.20,Http Status Code 401,在我的应用程序中,我使用Restkit将JSON响应映射到核心数据。我使用-,这样restkit将清理孤立对象 //孤立对象的清理 [[RKObjectManager sharedManager] addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) { // Value returned from the relativePath RKPathMatcher *pathMatcher = [RKPathMatcher pathM
[[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
文件列表请求失败,错误为:Error 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]];
删除错误映射后,Fetch请求块未被调用Fetch请求块仅用于成功的映射,如果401引发错误,则不应删除任何内容-如果您没有它的映射
RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)
覆盖4xx范围内的所有代码。您不需要使用它。如果您想忽略映射中的401
响应,则可以创建一个只包含400
(或400
,402
,等等)的索引集.Fetch请求块仅用于成功的映射,如果401出现错误,则不应删除任何内容。您的响应描述符/配置如何使用/处理401?@Wain谢谢您的评论,它让我找到了问题(请参阅“编辑”)。。