I read a code, it checks data and update UI every second. It sounds like what we usually do using NSTimer scheduledtimerwithtimeinterval. But this code is implemented with recursively calling dispatch_after like this:
- (void) retriggerMethod {
... do stuff here, assuming you want to do it on first invocation ...
dispatch_after( ..., ^{
[self retriggerMethod];
});
}
What's the difference between dispatch_after recursion and NSTimer scheduledtimerwithtimeinterval ? Is there potential risk when using the former? Cos I thought when you use it, the call stack would grow as long as not end this recursion.
NSTimer:
1. Need a NSRunloop.
2. Can repeat.
3. Can be invalid anytime if u want to cancel.
4. Can only run with delegate.
5. High level API.
dispatch_after:
1. Can be run everywhere that u want with dispatch_queue.
2. Can't repeat by itself.
3. Can't be cancel.
4. It run as block.
5. GCD.
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