10

I am using RestKit in my Objective-C project and need to specify a timeout for a call to my service of around 10 seconds.

After reading around, it doesn't look like RestKit supports this, so my plan is to:

  • Start a timer when my request is sent
  • When the data is loaded, disable the timer

Here's my problem...

If the timer method fires, I need to cancel the request and invoke the method below manually. I'm not 100% sure how to achieve this.

There's some context in my other question, showing how RestKit is implemented in my project and what it's doing in this case.

Many thanks in advance for any help you can give me on this one.

- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error { 
     NSLog(@"Hit error: %@", error); 
}
Community
  • 1
  • 1
Nick
  • 5,844
  • 11
  • 52
  • 98

3 Answers3

8

In RestKit version 0.20.x you can cancel scheduled requests using

[[RKObjectManager sharedManager]
    cancelAllObjectRequestOperationsWithMethod:RKRequestMethodAny
                           matchingPathPattern:YOUR_PATTERN];
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
5

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] 
}
mja
  • 5,056
  • 1
  • 29
  • 40
  • Thanks again mja. I'm getting Receiver type 'RKClient' for instance message does not declare a method with selector 'requestQueue' when I add the first method to my controller. I assume the cancelAfterTimeout is the selected method for the timer firing? – Nick Dec 01 '11 at 23:23
  • I don't know why you would get that error. From looking at the sources i can see requestQueue property in the RKClient.h header (https://github.com/RestKit/RestKit/blob/master/Code/Network/RKClient.h). Do you use recent version of RestKit? Yes, the cancelAfterTimeout is the selector to be send when timer fires. – mja Dec 01 '11 at 23:35
  • I'm using 0.9 (from the version file) – Nick Dec 01 '11 at 23:40
  • do you use the version from GitHub? Can you check if your RKClient.h does have the requestQueue property defined? – mja Dec 01 '11 at 23:43
  • The files are slightly different as I downloaded the latest build from the website about a week ago. I'll try updating. – Nick Dec 01 '11 at 23:48
  • i checked that the .zip version available on their homepage does not have this method implemented yet. I recommend you checking out the latest master branch. (here is very detailed guide as a reference https://github.com/RestKit/RestKit/wiki/Installing-RestKit-in-Xcode-4.x) – mja Dec 01 '11 at 23:55
  • I just managed to check out my first items using git. The error has now disappeared. Now this method call is failing [self handleRestKitError:myError]; (does not declare a method with selector...) my method is declared as you suggested - (void)handleRestKitError:(NSError *)error {... – Nick Dec 02 '11 at 00:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5512/discussion-between-nick-and-mja) – Nick Dec 02 '11 at 00:07
  • to 'fix' that one warning you need to declare your new selector in the header - just add - (void)handleRestKitError:(NSError*)error; in your controller's .h file – mja Dec 02 '11 at 00:38
1

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.

SoftDesigner
  • 5,640
  • 3
  • 58
  • 47