I'm trying to call an API and when my variables are ready, update UI components respectively.
This is my Network singleton who is launching the coroutine:
object MapNetwork {
fun getRoute(request: RoutesRequest,
success: ((response: RoutesResponse) -> Unit)?,
fail: ((throwable: Throwable) -> Unit)? = null) {
val call = ApiClient.getInterface().getRoute(request.getURL())
GlobalScope.launch(Dispatchers.Default, CoroutineStart.DEFAULT, null, {
try {
success?.invoke(call.await())
} catch (t: Throwable) {
fail?.invoke(t)
}
})
}
}
And this is how I call it:
network.getRoute(request,
success = {
// Make Some UI updates
},
fail = {
// handle the exception
})
And I get the Exception that says can't update UI from any thread other than UI thread:
com.google.maps.api.android.lib6.common.apiexception.c: Not on the main thread
I already tried this solution but resume
in Continuation<T>
class is "deprecated" since Kotlin 1.3
resume
is not deprecated, it just became an extension fun. – Raama