Reputation: 5051
I hope this to be a simple fix, you tell me.
What I have is a class IVAR *timer of type NSTimer. When an IBAction is performed, a method is called that contains this code:
if (timer) timer = nil;
timer = [NSTimer timerWithTimeInterval:0.2 target:self selector:@selector(removeOverlay) userInfo:nil repeats:NO];
In the same class, I have a method:
- (void)removeOverlay {
...
}
That isn't fired after the time interval of .2 seconds.
Might you know what the problem is here?
Upvotes: 0
Views: 137
Reputation: 8429
timer
the instance of NSTimer
is not released meanwhile.removeOverlay
looks like it updates some UI. Make shure you call it on main thread.removeLayout
method to see if it fires.timerWithTimeInterval
with scheduledTimerWithTimeInterval
To perform code on main thread (allows UI updating):
- (void)removeOverlay {
dispatch_async(dispatch_get_main_queue(), ^{
// update ui
});
}
More on Grand Central Dispatch (GCD)
Upvotes: 2
Reputation: 129
If it only gets called once I'd use [self performSelector:@selector(removeOverlay) afterDelay:(seconds you want to delay the use the method removeOverlay)];
Upvotes: 0
Reputation: 21966
Since no one mentioned it, an alternative to adding the timer to a run loop is to use the fire method:
[timer fire];
Upvotes: 1
Reputation: 1540
You need
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
The timers work by placing the calls at the appropriate points in the run loop.
Upvotes: 1