Hello, I'm actually trying to implement this but ...
# android
h
Hello, I'm actually trying to implement this but it is not working out. Can anyone help me with this? Thank you. https://stackoverflow.com/questions/54827455/how-to-implement-timer-with-kotlin-coroutines
l
Can you describe ‘not working out’? Is the flow not emitting values? Is it crashing? Syntax error?
I would recommend
Copy code
fun tickerFlow(period: Duration, initialDelay: Duration = Duration.ZERO) = flow {
    delay(initialDelay)
    while (true) {
        emit(Unit)
        delay(period)
    }
}
as suggested by one of the answers. It’s concise and should work fine.
h
I'm getting this java.lang.NoSuchMethodError: No static method delay-p9JZ4hM(JLkotlin/coroutines/Continuation;)Ljava/lang/Object; in class Lkotlinx/coroutines/DelayKt; or its super classes (declaration of 'kotlinx.coroutines.DelayKt'
And yes, app is crashing with above message
l
have you included
Copy code
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.3")
in the build gradle?
Looks like some jar is missing.
h
One more thing, when I'm implementing I'm getting @ExperimentalTime annotation Let me check that thing
Mine is 1.3.3
l
Try replacing delay(Duration) with delay(Long) and see if that works.
h
Ok, then, I have to change the value in the whole implementation as well, right?
l
Copy code
fun tickerFlow(period: Long, initialDelay: Long = 0) = flow {
    delay(initialDelay)
    while (true) {
        emit(Unit)
        delay(period)
    }
}
h
I tried and it worked, Thank you very much for guidance.