Reputation: 28248
Is there a way in iOS4 to call a method after a delay using something else other than NSThread or blocking the UI using sleep()?
Upvotes: 17
Views: 6041
Reputation: 15589
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(aMethod:) userInfo:nil repeats:NO];
- (void) aMethod:(NSTimer *) aTimer {
//task to do after the delay.
}
Upvotes: 1
Reputation: 1846
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// code to be executed on main thread.If you want to run in another thread, create other queue
});
Upvotes: 29
Reputation: 25692
[self performSelector:@selector(methodName) withObject:nil afterDelay:2.0];
Upvotes: 3
Reputation: 2976
You can easily do that with:
[self performSelector:@selector(methodName) withObject:nil afterDelay:5];
Upvotes: 0
Reputation: 137322
You can using NSTimer. (for example timerWithTimeInterval:target:selector:userInfo:repeats
)
Upvotes: 3
Reputation: 16540
- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
Of NSObject
will do this for you.
Additionally, you can create an NSTimer
and have it perform a callback to some target and selector after a given amount of time.
Upvotes: 9