1

我正在尝试激活不在主线程中的 scheduleTimerWithTimeInterval。这是我的代码。

-(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{

}

有什么想法吗?

4

2 回答 2

0
NSTimer *midnightTimer = [NSTimer scheduledTimerWithTimeInterval:secs 
                                                          target:self 
                                                        selector:@selector(midnightUpdate)  
                                                        userInfo:nil 
                                                         repeats:YES];
[midnightTimer fire];
于 2013-10-13T12:13:00.230 回答
0

可能的用途:

dispatch_after 将
一个块排入队列以在指定时间执行。

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

从另一个SO 答案

// 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>
});
于 2013-10-13T12:16:07.697 回答