scheduledTimerWithTimeInterval未被调用

时间:2013-10-13 11:59:24

标签: ios objective-c grand-central-dispatch nstimer

我正在尝试激活不在主线程中的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{

}

任何想法的人?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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>
});