I am trying to connect my iOS app to a GET data from my web service everytime something changes. My current implementation is to use NSTimer and do a ASIHttpRequest but I don’t like that polling implementation. Is there a better way to do it?
I am considering starting the request and lets it keep trying until the web service status code turns to OK (status 200) or perhaps a status code of “modified”. How would I do this in a view controller?
Here is what I have so far:
self.asiRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:self.barcodeUrl]];
[self.asiRequest setDelegate:self];
[self.asiRequest startAsynchronous];
[self.asiRequest addRequestHeader:@"Accept" value:@"application/json"];
[self.asiRequest addRequestHeader:@"Content-Type" value:@"application/json"];
int statusCode = [self.asiRequest responseStatusCode];
I know that if the request is successful, the delegate method - (void)requestFinished:(ASIHTTPRequest *)request
I guess my question is, how do I keep an open ASIHttpRequest such that when the web service returns “modified”, it calls a GET request and then keep an open connection again? This has to be asynchronous and not running on a UI thread as I don’t want to keep my app hanging.
Thanks much!
The easiest way to do this without having to do much on the client-side is to get your web service to send a
Location:header through when it returns “modified”, pointing to the URL you want toGETwith the new content. If the URL to monitor is different depending on e.g. user ID or the area of the app you are in, make a monitor script where you can pass inGETparams to configure what exact URL should be returned to you when “modified” is returned.The
ASIHTTPRequestwill then automaticallyGETthe content at the newLocation:and you can reschedule it inrequestFinished:to the monitor URL again, knowing that the response (if not empty/”Not Modified”) will ALWAYS be new data since the redirect to new data happens behind-the-scenes.