How to prematurely cancel a request in RestKit and invoke 'didFailWithError'
In RestKit version 0.20.x you can cancel scheduled requests using
[[RKObjectManager sharedManager] cancelAllObjectRequestOperationsWithMethod:RKRequestMethodAny matchingPathPattern:YOUR_PATTERN];
You can use cancelRequestsWithDelegate: selector in order to achieve the described workflow.
- (void)cancelAfterTimeout { [[[[RKObjectManager sharedManager] client] requestQueue] cancelRequestsWithDelegate:self]; NSError *myError = [[[NSError alloc] initWithDomain:NSPOSIXErrorDomain code:12345 userInfo:nil] autorelease]; //feel free to customize the error code, domain and add userInfo when needed. [self handleRestKitError:myError];}
However, it might be tricky to invoke that delegate error handler, but you can work around it by creating new, separate error handler like this:
- (void)handleRestKitError:(NSError*)error { //do something with the error}
and modify the body of your didFailWithError:
method:
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error { [self handleRestKitError:error] }
You can always end up with:[[RKObjectManager sharedManager].operationQueue cancelAllOperations];
Each of your requests will finish with error -999 (operation cancelled). You can check the error.code and perform appropriate action.