tags:

views:

1897

answers:

4

Is it possible to call my function in the background after certain interval programmatically in iPhone SDK development? I want to call one particular function in the background for certain time intervals(may be every 10 mins) during my app in on running..

Could you please share your ideas.

thanks.

Clave/

+4  A: 

You can have a timer, look into NSTimer for that that will fire off every 10 minutes, in order to make i t happen i n the background you have a few options ill name two. First of note that any UI work should not be done in another thread since UIKit is not thread safe.

NSThread reference here http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSThread%5FClass/Reference/Reference.html

NSTimer reference here http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSTimer%5FClass/Reference/NSTimer.html

Daniel
+2  A: 

To call the function on the main thread, use an NSTimer.

To call it on another thread, create an NSOperation and set up an NSOperationQueue to call it.

invalidname
thank you very much.
Clave Martin
+2  A: 

Easiest way is to schedule a NSTimer on the main threads run-loop. I suggest that the following code is implemented on your application delegate, and that you call setupTimer from applicationDidFinishLaunching:.

-(void)setupTimer;
{
  NSTimer* timer = [NSTimer timerWithTimeInterval:10 * 60
                                           target:self
                                         selector:@selector(triggerTimer:)
                                         userInfo:nil
                                          repeats:YES];
  [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

-(void)triggerTimer:(NSTimer*)timer;
{
  // Do your stuff
}

If your stuff here takes a long time, and you can not hold up the main thread then either call your stuff using:

[self performSelectorInBackground:@selector(myStuff) withObject:nil];

Or you could run the NSTimer on a background thread by with something like this (I am intentionally leaking the thread object):

-(void)startTimerThread;
{
  NSThread* thread = [[NSThread alloc] initWithTarget:self
                                             selector:@selector(setupTimerThread)
                                           withObject:nil];
  [thread start];
}

-(void)setupTimerThread;
{
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  NSTimer* timer = [NSTimer timerWithTimeInterval:10 * 60
                                           target:self
                                         selector:@selector(triggerTimer:)
                                         userInfo:nil
                                          repeats:YES];
  NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
  [runLoop addTimer:timer forModes:NSRunLoopCommonModes];
  [runLoop run];
  [pool release];
}

-(void)triggerTimer:(NSTimer*)timer;
{
  // Do your stuff
}
PeyloW
Generally NSTimers should only be scheduled on threads that you can be certain have a runloop (there is no guarantee performSelectorInBackground will call the selector on a thread that has a runloop). For manually created threads or performSelectorInBackground, it is correct to either launch the thread immediately and sleep, or schedule the timer on the main thread, and then launch the background thread only when the timer fires.
rpetrich
A: 

IPHONE OS 4 : can we Run NSTimer in background .... if not specify me some alternate methods

ramkumar