前幾天使用dispatch_source_t做計時器铸鹰,遇到了dispatch_source_set_event_handler 不執(zhí)行的問題
代碼如下
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒執(zhí)行
dispatch_source_set_event_handler(_timer, ^{
dispatch_async(dispatch_get_main_queue(), ^{
//設(shè)置界面的按鈕顯示 根據(jù)自己需求設(shè)置
timeCount=timeCount+2;
DLog(@"dasd%d",timeCount);
});
});
dispatch_resume(_timer);
大致是沒有問題得 但是 dispatch_source_set_event_handler 是不執(zhí)行的,
原因可能是被釋放的問題
將_timer 聲明為屬性就沒有這個問題了