0

I'm trying to get address from lat/lng using Google geocoder API, getting some random crashes, here are the logs for the crash

Fatal Exception: android.os.NetworkOnMainThreadException:
       at android.os.Parcel.createException(Parcel.java:1952)
       at android.os.Parcel.readException(Parcel.java:1910)
       at android.os.Parcel.readException(Parcel.java:1860)
       at android.location.ILocationManager$Stub$Proxy.getFromLocation(ILocationManager.java:949)
       at android.location.Geocoder.getFromLocation(Geocoder.java:133)
       at com.ui.activities.map.MapViewModel$getAddress$1.invokeSuspend(MapViewModel.kt:21)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
       at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:367)
       at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
       at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
       at kotlinx.coroutines.CoroutineStart.invoke(CoroutineStart.kt:110)
       at kotlinx.coroutines.AbstractCoroutine.start(AbstractCoroutine.kt:126)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch(Builders.common.kt:56)
       at kotlinx.coroutines.BuildersKt.launch(BuildersKt.java:1)
       at kotlinx.coroutines.BuildersKt__Builders_commonKt.launch$default(Builders.common.kt:47)
       at kotlinx.coroutines.BuildersKt.launch$default(BuildersKt.java:1)
       at com.ui.activities.map.MapViewModel.getAddress(MapViewModel.kt:19)
       at com.ui.activities.MapActivity.reload(MapActivity.java:1275)
       at com.ui.activities.MapActivity.onMapReady(MapActivity.java:1259)
       at com.google.android.gms.maps.zzat.zzb(com.google.android.gms:play-services-maps@@18.0.0:1)
       at com.google.android.gms.maps.internal.zzaq.zza(com.google.android.gms:play-services-maps@@18.0.0:5)
       at com.google.android.gms.internal.maps.zzb.onTransact(com.google.android.gms:play-services-maps@@18.0.0:3)
       at android.os.Binder.transact(Binder.java:667)
       at fd.c(fd.java:2)
       at com.google.maps.api.android.lib6.impl.bg.run(bg.java:1)
       at android.os.Handler.handleCallback(Handler.java:873)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:193)
       at android.app.ActivityThread.main(ActivityThread.java:6669)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Here is my solution, calling geocoder getlocation from inside coroutine, but still getting some crashes,

fun getAddress(latitude: Double, longitude: Double) {
    val geocoder = Geocoder(app, Locale.getDefault())
    viewModelScope.launch {
        try {
            val addresses = geocoder.getFromLocation(latitude, longitude, 1)  
            addressObserver.value = addresses
        }catch (ex: Exception){
            ex.printStackTrace()
        }

    }
}
1
  • viewModelScope.launch() will default to using Dispatchers.Main.immediate. You should specify your own dispatcher. Commented Dec 1, 2022 at 17:30

1 Answer 1

0

A coroutine launched from viewModelScope runs on the Main dispatcher by default.

In your case, as far as I know, there are two options to solve this:

  1. viewModelScope.launch(Dispatchers.IO) {} should be used.
  2. Keep using viewModelScope.launch {} and wrap the getFromLocation() inside withContext(Dispatchers.IO) {}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.