swift-debouncer
swift-debouncer copied to clipboard
Silent Failure if not called from Main Thread
It's common practice in Swift to use GCD with a serial queue to replace the @synchronized
primitive. Thus, if this is being used from within that code, it won't be on the Main Thread. NSTimer/Timer
usage here expects there to be a RunLoop present on the current thread, but that's only generally true of the Main Thread (unless you added one yourself). The current unmodified behaviour when used on a non-Main Thread will be to silently not issue your callback after the delay.
Thus I'd recommend changing this slightly to ensure that if (and only if) not on the Main Thread, the Timer.schedule...
call is issued inside a dispatched call with DispatchQueue.main.async {}
.
Let me know if you'd support a fork/MR with that, I have one I'm currently using that works for me in this use case.