7
-(void)viewDidLoad{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [NSTimer scheduledTimerWithTimeInterval:0.10 
                                         target:self 
                                       selector:@selector(action_Timer) 
                                       userInfo:nil 
                                        repeats:YES];
        }
    );
}

-(void)action_Timer{
    LOG("Timer called");
}

action_Timer is not being called. I dont know why. Do you have any idea?

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
user2019279
  • 163
  • 2
  • 7
  • I have tried following code also,n [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; and still having the same issue :( . – user2019279 Jan 28 '13 at 19:50
  • Related: http://stackoverflow.com/questions/12323531/difference-in-scheduling-nstimer-in-main-thread-and-background-thread – iDev Jan 28 '13 at 20:00

2 Answers2

19

You're calling +[NSTimer scheduledTimerWithTimeInterval:...] from a GCD worker thread. GCD worker threads don't run a run loop. That's why your first try didn't work.

When you tried [[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode], you were sending a message to the main run loop from a GCD worker thread. The problem there is NSRunLoop is not thread-safe. (This is documented in the NSRunLoop Class Reference.)

Instead, you need to dispatch back to the main queue so that when you send the addTimer:... message to the main run loop, it's done on the main thread.

-(void)viewDidLoad {
    [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSTimer *timer = [NSTimer timerWithTimeInterval:0.10 
                                         target:self 
                                       selector:@selector(action_Timer) 
                                       userInfo:nil 
                                        repeats:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        });        
    });
}

Realistically, there's no reason to create the timer on the background queue if you're going to schedule it in the main run loop. You can just dispatch back to the main queue to create and schedule it:

-(void)viewDidLoad {
    [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"on background queue");
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"on main queue");
            [NSTimer scheduledTimerWithTimeInterval:0.10 
                                         target:self 
                                       selector:@selector(action_Timer) 
                                       userInfo:nil 
                                        repeats:YES];
        });        
    });
}

Note that both of my solutions add the timer to the main run loop, so the timer's action will run on the main thread. If you want the timer's action to run on a background queue, you should dispatch to it from the action:

-(void)action_Timer {
    // This is called on the main queue, so dispatch to a background queue.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        LOG("Timer called");
    });
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • I am doing the same thing, but i need to stop the `timer` when application goes into background. But it's not working for me. – Hemant Singh Rathore Oct 21 '13 at 13:03
  • This works fine if the app is in foreground, but what if the app is in background? The dispatch_get_main_queue doesn't run. Right? – superpuccio May 02 '16 at 14:35
  • Apps don't normally run at all in the background. You need to look into [“Background Execution”](https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html) or use a [local notification](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html#//apple_ref/doc/uid/TP40008194-CH102-SW9) if you want to a timer to run in the background. – rob mayoff May 02 '16 at 15:50
1

You have to add the timer to the main run loop for the timer to fire, but first, you should hold reference to to the timer in a private ivar or a property:

-(void)viewDidLoad{
        // on the main queue
        self.mytimer = [NSTimer scheduledTimerWithTimeInterval:0.10 
                                                        target:self 
                                                      selector:@selector(action_Timer) 
                                                      userInfo:nil 
                                                        repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
}

-(void)action_Timer{
    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        LOG("Timer called");
    });
}

I find easier to get off the main queue in the called method. At somepoint, maybe in viewDidUnlod or in dealloc, you will have call [self.myTimer invalidate]; self.myTimer = nil;.

Mike D
  • 4,938
  • 6
  • 43
  • 99
  • The problem with your solution is that `NSRunLoop` is not thread-safe. You must only send messages to the main run loop on the main thread/queue. You don't need to hold a private reference to a scheduled timer, because the run loop holds a reference to it. – rob mayoff Jan 28 '13 at 20:10
  • Mike , action_Timer is still not calling. I replicate your code. but no luck. – user2019279 Jan 28 '13 at 20:12
  • @user2019279 I have tested the above code, and it is working as expected. – Mike D Jan 28 '13 at 20:23