How to run NSTimer in background Thread
You can use GCD dispatch queue for BackGround thread :=
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
NSTimer *timer = [NSTimer timerWithTimeInterval:0.5
target:self
selector:@selector(timerFired)
userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
});
});
Krish, you seem to be getting yourself on a wrong path here.
First, you should create timers while on the main thread by just calling scheduledTimerWithTimeInterval. Putting the call scheduledTimerWithTimerInterval onto an operationQueue is pointless. Timers are called from their thread's runloop. Trying to dispatch them with a different operationQueue will get you into trouble.
If you want the actual operation of a timer to happen in the background, then use dispatch_async (...) in your timer callback method to run code in the background.
Unless you have a legitimate reason to do otherwise, schedule your timer on the main thread.
Your callback method should have a parameter for the timer. That way your callback method can manipulate the timer, for example by invalidating it. scheduledTimer... also returns the timer. Usually you would store that timer object so that you can invalidate it when your view disappears. If you don't do that, you will get a new NSTimer every time viewDidLoad is called. That is you will have more and more timer callbacks running as your app is running.
Learn GCD (grand central dispatch). It is much, much simpler than operation queues. You shouldn't use operation queues unless you have a real good reason.
When you ask "how do we stop the background thread" - you don't. You dispatch code to it, the code runs, and as long as there is code dispatched, it will run. If there is no code dispatched the the background, it stops running until you dispatch code again. That's how it is supposed to work.
If you meant "how do I stop the timer" - that's what invalidate does.
PS. If you wanted to schedule a timer from a background thread (which you don't want, believe me), a correct answer can be found here: iOS5 ARC is it safe to schedule NSTimers from background selectors?