Question

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

Was it helpful?

Solution

Fetch request blocks are only used on successful mappings, if the 401 raises an error then nothing should be deleted - if you don't have a mapping for it.

RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError) covers all of the codes in the 4xx range. You don't need to use that. If you want to ignore the 401 response from the mapping then you can create an index set containing only 400 (or 400, 402, etc as required).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top