8

I want to run a task in the background while app is active and running. The task should be executed repeatedly in every 2 minutes or so. If the application is inactive then the 'background repeated task' should be paused and it should continue if the app is active again. What is the best way to do this ? Please help.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Use NSTimer. http://stackoverflow.com/questions/1449035/how-do-i-use-nstimer and http://stackoverflow.com/questions/9975562/how-to-pause-play-nstimer – Prasad_R Sep 18 '13 at 04:31

1 Answers1

10

You can try like this.. Write the following line at where do you want to call the timer..

 NSTimer *aTimer = [NSTimer scheduledTimerWithTimeInterval:10.0
                                          target:self 
                                        selector:@selector(timerFired:) 
                                        userInfo:nil 
                                         repeats:YES];
 [aTimer fire];


-(void)timerFired:(NSTimer *) theTimer
  {    
     //Do you your work here..
  }

You can also stop the timer with [aTimer invalidate]; aTimer=nil; this code.

And also you can check this link https://stackoverflow.com/questions/11058571/nstimer-timerwithtimeinterval

Community
  • 1
  • 1
kalyani puvvada
  • 496
  • 4
  • 12