I am relatively new to Co-Routines and I am trying get the behaviour what the Launch co-routine would accomplish :
launch(UI) {
val v1 = someDeferredType
val v2 = v1.await()
val v3 = v2.text
}
In the above example v3 will wait for v2 to execute and then run while not blocking the main thread. While this is great, this brings in the Deferred Type and Co-routine logic in my Calling Activity/Fragment.
I would like to keep my Activity/Fragment free from specific implementation details, something like this :
fun getResponseString() : String {
launch(UI) {
val v1 = someDeferredType
val v2 = v1.await()
val v3 = v2.text
}
return v3 //This is the actual String which I need to be returned
}
So that I can just call getResponseString() like a regular function from my activity.
The only option I have come across so far is to use runBlocking co-routine but thats blocks the main thread altogether unlike launch.
Maybe I am missing something or is it not possible to do something like this using Co-routines in Kotlin ?