Any one know how to call api in every 5 second usi...
# android
m
Any one know how to call api in every 5 second using kotlin coroutine?
c
Copy code
while (true) {
  doStuff()
  delay(5000)
}
?
m
Copy code
fun main(array : Array<String>){
    val job = repeatFun()
    job.start()
}

fun repeatFun(): Job {
    val coroutineScope = CoroutineScope(Dispatchers.Default)
    return coroutineScope.launch {
        while(isActive) {
            println("Hello")
            delay(5000)
        }
    }
}
I have did something like this but it is not printing anything.
c
When the main method ends, the program ends.
Copy code
suspend fun main() {
  // Your code here
  delay(100000)
}
That will work
Also, you don't need to call
start
.
m
Yes but it is not running
c
It is running, but when the program ends at the end of
main
, it gets killed.
Copy code
suspend fun main() {
  val job = GlobalScope.launch {
    delay(1000)
    println("This will never be printed")
  }
  println("This is printed, then the program dies")
}
Copy code
suspend fun main() {
  val job = GlobalScope.launch {
    delay(1000)
    println("This is printed after 1 second")
  }.join()
}
c
#coroutines