2011-01-25 3 views
0

GCDタイマーは、実行予定のもの以外のキューから中断できますか?main_queueからディスパッチタイマーを別のマシンで作成したときに、ディスパッチタイマーを中断する

私は、優先度の低いglobal_queueで作成されたタイマーを持っています。起動すると、main_queue経由でUIの操作をいくつか操作します。 UIのいくつかの州では、タイマーを一時停止する必要があります。サスペンドを実行するには、main_queueから優先度の低いキューに切り替える必要がありますか?

dispatch_queue_t lowPriQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); 
myTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, lowPriQ);   

dispatch_source_set_timer(myTimer, 
          startTime, // now 
          interval, // 15 seconds 
          2000ull); 

// configure the event handler 
dispatch_source_set_event_handler(myTimer, ^{   
    NSLog(@"Timer fired"); 

    // UI Work 
    dispatch_async(dispatch_get_main_queue(),^{ 
      [self doSomeButtonEnableDisable] 
    });       
}); 

dispatch_resume(myTimer); // start the timer 

- (void)doSomeButtonEnableDisable 
{ 
    if (someParticularState) { 
     // Turn off the timer 

     // Should I suspend the timer on the low priority global queue 
     // or is it valid to suspend on the main queue? 

     dispatch_queue_t lowPriQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); 

     dispatch_async(lowPriQ(),^{ 

      dispatch_suspend(myTimer); 
     });        
    } 
} 

答えて

1

はい、任意のキューからディスパッチオブジェクトを中断することは有効です。 dispatch_suspend()が呼び出されたときにブロックが現在実行中の場合、そのブロックは実行を完了し、その後のスケジュールされたブロックは実行されません。

関連する問題