In my app I use Restkit to map the JSON response into core data. I use - addFetchRequestBlock - so that restkit will cleanup orphaned objects.
// Cleanup of orphaned objects
[[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;
}];
All works well, but if I have an auth error 401
File List Request Failed with Error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Loaded an unprocessable error response (401)
all the managed objects in the "/:folderId" are treated as orphaned objects and gets deleted.
Is there a way to prevent this behaviour and not perform an orphaned objects cleanup?
EDIT -
Seems I was mapping the error
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]];
after deleting the error mapping, the Fetch Request Block was not called