Hello all. How do I call a suspend function in App...
# ktor
r
Hello all. How do I call a suspend function in Application.kt? I have a suspend fun and when I call it, an error appears forcing to change the main function to suspend. And if I do that, I can't start the application.
a
if you are using coroutines in your app, it is a good practise to start coroutine fron the main function because it incorporates the coroutine and the dispatcher can be reused. Try:
Copy code
suspend fun main() = withContext(Dispatchers.Default) {
    // put your main's stack here
    // here you can call suspend funs 
    // and thread main is using when suspended 
    // other coroutine can work on it 
    // environment is reusable
}
r
Thanks @Animesh Sahu, but the Ktor does not accept susped fun main. But the Application.module inherits CoroutineScope, so just do
launch {asynchronous fun}
a
@Rodrigo Silva what do you mean?
r
Screenshot_20200209_105308.png
It worked that way. The way you said,
suspend fun main
, ktor doesn't accept.
139 Views