1

i am trying to active scheduledTimerWithTimeInterval not in the main thread. this is my code.

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

}

Any idea guys?

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Sosily
  • 706
  • 1
  • 10
  • 26

2 Answers2

0
NSTimer *midnightTimer = [NSTimer scheduledTimerWithTimeInterval:secs 
                                                          target:self 
                                                        selector:@selector(midnightUpdate)  
                                                        userInfo:nil 
                                                         repeats:YES];
[midnightTimer fire];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Magyar Miklós
  • 4,182
  • 2
  • 24
  • 42
0

Possible use:

dispatch_after
Enqueue a block for execution at the specified time.

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

from another 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>
});
Community
  • 1
  • 1
zaph
  • 111,848
  • 21
  • 189
  • 228