Vivek Modi
08/09/2021, 1:13 PMapi
from object
class. I am new in Coroutines. That API call should be executer concurrently and app's flow should not wait for result of that call. I tried some code, but i am not sure is it correct way of doing it or not. Also i got a error`Process: com.dimen.app, PID: 12496`
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1605)
private fun setupLogout() {
logoutButton.setOnClickListener {
LoginHelper.logout()
}
}
fun logout() {
logD("logout")
deleteSession()
.... more function
}
private fun deleteSession() {
runBlocking {
Tokenclass.getToken()?.let {
logE("token ::-> $it")
apiCall.deleteSession(it).execute()
}
}
}
ursus
08/09/2021, 6:41 PMVivek Modi
08/09/2021, 6:43 PMNick Allen
08/09/2021, 9:26 PMinterface MyApi {
@DELETE("sessions/delete/{id}")
suspend fun deleteSession(@Path("id") sessionId: String)
}
You should not be calling runBlocking from main thread. Code is likely to hang or ANR. Generally, you want to start coroutines using lifecycleScope.launch { ... }
. This starts the coroutine and then returns immediately without waiting for the coroutine to finish.