views:

226

answers:

4

Hello,

In my iPhone app I would like to run several queries when the application is in background. I already use ASIHttpRequest to make the queries, that works fine but now I try to find a way to trigger them in background. In the app delegate, I have added a call to the method making the request:

   [self getItemsFromServer]

getItemsFromServer runs an asynchronous request (on the simulator I saw the log of this methods once I get back the application to the foreground). How can I use some kind of timer to have this method ran every 10 minutes (I just need to run it 4 or 5 times, not each 10 minutes until it goes back to foreground :-) )?

thanks a lot,

Best Regards,

Luc

A: 

You could probably use Task Finishing to do that. In iOS you can mark a thread as finishing and give it a specific time to live. This would let you do a few more calls to your web server.

Have a look at Executing Code in the Background

willcodejavaforfood
A: 

Actually, you are specially not allowed to make general HTTP calls while in background. The only apps that can be active in the background are those that play audio, do location or are running VOIP calls. I believe Apple's whole philosophy with background is that apps shouldn't be doing 'work' other than these limited cases because there are limited resources available. THe suggested way to work around this is to use (ugh) notifications or just do a refresh when your application wakes up. The doc that willcodejavaforfood references explains this.

joelm
@joelm This is not entirely true (from what I perceive at [Completing a Long-Running Task in the Background](http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH5-SW6)).
RoLYroLLs
+1  A: 

iOS4 allows your app to run for X amount of time, granted that iOS4 grants you the time you request. Check out: Completing a Long-Running Task in the Background.

Specifically,

Any time before it is suspended, an application can call the beginBackgroundTaskWithExpirationHandler: method to ask the system for extra time to complete some long-running task in the background. If the request is granted, and if the application goes into the background while the task is in progress, the system lets the application run for an additional amount of time instead of suspending it

RoLYroLLs
I'll definitely try this. thanks a lot
Luc
You are welcome! =)
RoLYroLLs
A: 

If I use ASIHttprequest will my app is rejected? Is ASIHTTPRequest is a private class?Will apple accept this class?

Dinesh Kumar
-1 Not an answer.
tc.
How to send http request when app is in background:
Dinesh Kumar
I tried NSTimer *timer = [[NSTimer timerWithTimeInterval:10.0 target:self selector:@selector(test) userInfo:nil repeats:NO ] retain]; runLoop = [NSRunLoop currentRunLoop]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; -(void)test{ NSURL *url = [NSURL URLWithString:@"http://192.168.1.34:8888/webbug.php"]; ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous];
Dinesh Kumar
but this is not working in background....
Dinesh Kumar
Help me How to send a http request in background....
Dinesh Kumar
If you have a question, please post it in it's own thread, instead of thread-hijacking, and you just might get the answer you are looking for. Thanks.
RoLYroLLs