https://kotlinlang.org logo
#ktor
Title
# ktor
r

Rodrigo Silva

02/08/2020, 9:38 PM
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

Animesh Sahu

02/09/2020, 5:46 AM
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

Rodrigo Silva

02/09/2020, 2:46 PM
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

Animesh Sahu

02/09/2020, 2:52 PM
@Rodrigo Silva what do you mean?
r

Rodrigo Silva

02/09/2020, 2:53 PM
Screenshot_20200209_105308.png
It worked that way. The way you said,
suspend fun main
, ktor doesn't accept.
99 Views