2013-10-13 47 views
1

我正在嘗試激活scheduledTimerWithTimeInterval不在主線程中。 這是我的代碼。scheduledTimerWithTimeInterval不叫

-(void)setMidnightUpdate{ 

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 
              0), ^{ 

     if(midnightTimer && [midnightTimer isValid]) { 
      [midnightTimer invalidate]; 
      midnightTimer = nil; 
     } 

secs = 10; 
midnightTimer = [NSTimer scheduledTimerWithTimeInterval:secs 
               target:self 
               selector:@selector(midnightUpdate) 
               userInfo:nil 
               repeats:NO]; 

});

-(void)midnightUpdate{ 

} 

任何想法的傢伙?

回答

0
NSTimer *midnightTimer = [NSTimer scheduledTimerWithTimeInterval:secs 
                  target:self 
                 selector:@selector(midnightUpdate) 
                 userInfo:nil 
                 repeats:YES]; 
[midnightTimer fire]; 
0

可能使用:

dispatch_after
排隊用於在指定的時間執行的塊。

void dispatch_after(
    dispatch_time_t when, 
    dispatch_queue_t queue, 
    dispatch_block_t block); 

從另一個SO answer

// Delay execution of my block for 10 seconds. 
dispatch_time_t futureTime = dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC); 
dispatch_queue_t aQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 
dispatch_after(futureTime, aQueue, ^{ 
    < midnightUpdate code> 
}); 
+0

感謝的人!它工作完美! – Sosily