iphoneobjective-cmacostimercfrunloop

NSTimer setFireDate


Is considered thread-safe to call setFireDate: from another thread than the one in which the timer is scheduled? I mean, I detach this function in a new thread:

-(void)CFRunLoopTest {

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:YES];

    runLoop = CFRunLoopGetCurrent();

    CFRunLoopAddTimer(runLoop, (CFRunLoopTimerRef)timer, kCFRunLoopCommonModes);

    CFRunLoopRun();
    [pool drain];
}

May I call [timer setFireDate:] from the main thread? I did not found anything in documentation that forbids it...


Solution

  • A note from the NSTimer reference for setFireDate: method says

    You could potentially call this method on a non-repeating timer that had not yet fired, although you should always do so from the thread to which the timer is attached to avoid potential race conditions.

    Also see if the following Discussion helps.