I want to execute a particular function after some timeout or if some particular condition is met. I have done the same in swift using DispatchWorkItem
and used
dispatchQueue?.asyncAfter(deadline: .now() + .seconds(10), execute: self.dispatchWorkItemForDevicesDiscovery!)
to start the timer and after 10 seconds the associated disptachWorkItem gets executed.
How to do that in Kotlin?
You can use Kotlin's coroutines for that. You can create your own suspend function which checks a given condition any amount x of time.
You will need to add a dependency because coroutines are not part of the standard library.
Here is what you need to put in your pom.xml (for Maven):
Additionally you need to activate them:
Further reading