How can I delay a method call for 1 second?
Solution 1:
performSelector:withObject:afterDelay:
Document Reference
Solution 2:
You could also use a block
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[object method];
});
Most of time you will want to use dispatch_get_main_queue, although if there is no UI in the method you could use a global queue.
Edit:
Swift 3 version:
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
object.method()
}
Equally, DispatchQueue.global().asyncAfter(...
might also be a good option.
Solution 3:
Best way to do is :
[self performSelector:@selector(YourFunctionName)
withObject:(can be Self or Object from other Classes)
afterDelay:(Time Of Delay)];
you can also pass nil as withObject parameter.
example :
[self performSelector:@selector(subscribe) withObject:self afterDelay:3.0 ];