1

実行するようにスケジュールされているもの以外のキューからGCDタイマーを一時停止できますか?

優先度の低い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);
        });                            
    }
}
4

1 に答える 1

2

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

于 2011-01-25T23:33:52.823 に答える