@@ -32,11 +32,10 @@ #import "OFInvalidArgumentException.h" @implementation OFTimer @synthesize timeInterval = _interval, repeating = _repeats, valid = _valid; -@synthesize of_inRunLoop = _inRunLoop; + (instancetype)scheduledTimerWithTimeInterval: (of_time_interval_t)timeInterval target: (id)target selector: (SEL)selector repeats: (bool)repeats @@ -488,10 +487,11 @@ /* * The run loop references the timer, so it should never be deallocated * if it is still in a run loop. */ assert(_inRunLoop == nil); + assert(_inRunLoopMode == nil); [_fireDate release]; [_target release]; [_object1 release]; [_object2 release]; @@ -516,10 +516,23 @@ timer = (OFTimer *)object; return [_fireDate compare: timer->_fireDate]; } + +- (void)of_setInRunLoop: (OFRunLoop *)runLoop + mode: (of_run_loop_mode_t)mode +{ + OFRunLoop *oldInRunLoop = _inRunLoop; + of_run_loop_mode_t oldInRunLoopMode = _inRunLoopMode; + + _inRunLoop = [runLoop retain]; + [oldInRunLoop release]; + + _inRunLoopMode = [mode copy]; + [oldInRunLoopMode release]; +} - (void)fire { void *pool = objc_autoreleasePoolPush(); id target = [[_target retain] autorelease]; @@ -532,10 +545,11 @@ if (_repeats && _valid) { int64_t missedIntervals = -[_fireDate timeIntervalSinceNow] / _interval; of_time_interval_t newFireDate; + OFRunLoop *runLoop; /* In case the clock was changed backwards */ if (missedIntervals < 0) missedIntervals = 0; @@ -544,11 +558,13 @@ [_fireDate release]; _fireDate = [[OFDate alloc] initWithTimeIntervalSince1970: newFireDate]; - [[OFRunLoop currentRunLoop] addTimer: self]; + runLoop = [OFRunLoop currentRunLoop]; + [runLoop addTimer: self + forMode: [runLoop currentMode]]; } else [self invalidate]; #ifdef OF_HAVE_BLOCKS if (_block != NULL) @@ -609,17 +625,19 @@ [self retain]; @try { @synchronized (self) { OFDate *old; - [_inRunLoop of_removeTimer: self]; + [_inRunLoop of_removeTimer: self + forMode: _inRunLoopMode]; old = _fireDate; _fireDate = [fireDate copy]; [old release]; - [_inRunLoop addTimer: self]; + [_inRunLoop addTimer: self + forMode: _inRunLoopMode]; } } @finally { [self release]; } }