Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're calling <code>+[NSTimer scheduledTimerWithTimeInterval:...]</code> from a GCD worker thread. GCD worker threads don't run a run loop. That's why your first try didn't work.</p> <p>When you tried <code>[[NSRunLoop mainRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode]</code>, you were sending a message to the main run loop from a GCD worker thread. The problem there is <code>NSRunLoop</code> is not thread-safe. (This is documented in the <a href="http://developer.apple.com/library/ios/#Documentation/Cocoa/Reference/Foundation/Classes/NSRunLoop_Class/Reference/Reference.html#//apple_ref/occ/instm/NSRunLoop/addTimer:forMode:" rel="noreferrer"><em>NSRunLoop Class Reference</em></a>.)</p> <p>Instead, you need to dispatch back to the main queue so that when you send the <code>addTimer:...</code> message to the main run loop, it's done on the main thread.</p> <pre><code>-(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]; }); }); } </code></pre> <p>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:</p> <pre><code>-(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]; }); }); } </code></pre> <p>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:</p> <pre><code>-(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"); }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload