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()?
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
});
- (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.
You can using NSTimer. (for example timerWithTimeInterval:target:selector:userInfo:repeats
)
[self performSelector:@selector(methodName) withObject:nil afterDelay:2.0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With