I'm working on a Kotlin multiplatform application ...
# getting-started
a
I'm working on a Kotlin multiplatform application targeting android and desktop. Now in desktop module what i want to do is create a class which will have three functions namely start, restart and stop. Now, what I want is that when I call the start function a task should be scheduled which calls an action function outside the class after certain amount of time. The restart function should first check if a scheduled task is already active, if yes then cancel it. Then start a similar delayed task as the start function. The stop function should check if a scheduled task if running, is yes then cancel it. Example- Considering a delay of 2 minutes If I first call start() and after 1 minute call restart() then the action function should be executed only once i.e., at 3 minutes. How can I do this in Kotlin?
j
Depending on your requirements, if you only need the tasks to run while the app is running in the foreground, you can use coroutines to schedule and run work in the future, as well as cancel a coroutine job. Depending on how long your background tasks run, Android may kill your app if it's in the background. So you might need to use something like WorkManager on Android. You can share the coroutines related code, but
WorkManager
is Android only, so you may need to do some implementations in platform source sets with `expect`/`actual`.
­čĹÇ 1