https://kotlinlang.org logo
#coroutines
Title
# coroutines
o

otakusenpai

01/20/2019, 9:59 AM
Hey guys, I have a function inside a coroutinescope which I need to call every few seconds. How do I do that? It's a timer kind of thing.
This was what I used earlier, but now I dont remember what I did to make it work... https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.concurrent/timer.html
I don't think
timer
is coroutine-aware so I don't think you can use that & properly suspend
o

otakusenpai

01/20/2019, 10:26 AM
Oh yes ticker channels...thats what I used last time
Thnx
On a sidenote, how do I pass the function that I want to work on to a ticker channel ?
o

octylFractal

01/20/2019, 11:13 AM
you don't, you would just consumer the channel to time it
assuming you have `val channel = ticker(...)`:
Copy code
for (delay in channel) {
     functionToRun()
}
o

otakusenpai

01/20/2019, 11:19 AM
ok let me see
ok Im having this one problem. The function Im using send a message over a network, and uses a flood locking mechanism to stop flooding. Now when I try to use this
Copy code
val one = async {
for(delay in whoUpdater) {
                            logger.log(toLog, toFile,"$botname: Turning on channel sync")
                            updateChanList()
                            logger.log(toLog, toFile,"$botname: Done syncing")
                        }
}
one.await()
Then it justs listens for that message instead of any other message. How do i stop it from happening?
I could put a small break statement inside the for loop but idk
Ok I put a break inside tht for loop but no change.... Please guide me
d

Dico

01/20/2019, 2:40 PM
I don't understand the problem
Instead of
async
I think you should use
launch
And don't
await
, what are you awaiting
4 Views