I am reading basics of coroutines from docs, I hav...
# coroutines
v
I am reading basics of coroutines from docs, I have a doubt like :
Copy code
launch {
   doingNetworkCall()  // this is suspend function
}
// doing some work on main thread here like loading
so assume doingNetworkCall() is taking some time, till that time that function get suspended OR it just works in background thread and on completion it returns like callback and do other work and if this work is getting suspended, then when it is getting resumed to achieve desired output
c
You will want to run everything in the same coroutine (one
launch { }
block) and switch contexts as necessary to ensure things get run on the proper threads.
Copy code
launch { 
    withContext(Dispatchers.Main) {
        showProgressBar()
    }
    val resultFromApiCall = withContext(<http://Dispatchers.IO|Dispatchers.IO>) { 
        doingNetworkCall()
    }
    withContext(Dispatchers.Main) { 
        dismissProgressBar()
        updateUiWithInfoFromApi(resultFromApiCall)
    }
}
v
oh okay thanks
l
Note that this would probably crash your app when there's no internet, unless you catch the possible exceptions (but not
CancellationException
)
v
yeah okay, I am taking just an example