Answers for "kotlin run task every second"

0

kotlin run task every second

val scope = MainScope() // could also use an other scope such as viewModelScope if available
var job: Job? = null

fun startUpdates() {
    stopUpdates()
    job = scope.launch {
        while(true) {
            getData() // the function that should be ran every second
            delay(1000)
        }
    }
}

fun stopUpdates() {
    job?.cancel()
    job = null
}
Posted by: Guest on December-28-2021

Code answers related to "kotlin run task every second"

Browse Popular Code Answers by Language