Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Location Fetching fails too many times #169

Open
Vaibhav2002 opened this issue Jan 24, 2025 · 3 comments
Open

Location Fetching fails too many times #169

Vaibhav2002 opened this issue Jan 24, 2025 · 3 comments

Comments

@Vaibhav2002
Copy link

Vaibhav2002 commented Jan 24, 2025

Hi,
I am using Compass 1.2.4 in my Kotlin Multiplatform + Compose Multiplatform app which is on production for quite some time
We're getting alot of complaints that location fetching is not working
After checking I found out there are soo many failures

This is in the last 90 days
Any idea what could be going wrong

Image

Also the "Device does not support geolocation" failure is happening on devices which do support geolocation, as location fetching works in every other app on that device.

This is how I am using Compass

private fun fetchLocation() = flow { emit(geoLocator.current()) }
    .onStart { isFetchingLocation = true }
    .onEach { isFetchingLocation = false }
    .onEach {
        if (it is GeolocatorResult.Success) return@onEach
        when (it as GeolocatorResult.Error) {
            is GeolocatorResult.PermissionDenied -> showLocationDialog = true
            else -> throw Exception(it.message)
        }
    }
    .filterIsInstance<GeolocatorResult.Success>()
    .mapLatest { geocoder.reverse(it.data.coordinates) }
    .onEach { if (it.isError) throw Exception(it.errorOrNull().toString()) }
    .filterIsInstance<GeocoderResult.Success<Place>>()
    .mapLatest { it.data.firstOrNull() ?: throw Exception(it.errorOrNull().toString()) }
    .onEach {
        updateUser {
            copy(
                location = it.locality.plus(", ").plus(it.administrativeArea)
            )
        }
    }
    .onEach { showSuccessSnackBar("Location updated successfully") }
    .safeCatch { showErrorSnackBar(it); isFetchingLocation = false }
    .launchIn(componentScope)
@jordond
Copy link
Owner

jordond commented Jan 24, 2025

At least on Android current() can timeout and return null. I believe iOS does the same thing, but I think it has a much longer timeout period.

You can switch to using locationUpdates: Flow<Location> or trackingStatus: Flow<TrackingStatus>, then calling track and listen to the Flow.

Or you can call startTracking(). Which is a suspend fun that calls track() and returns the first value. (You will still have to call stopTracking() afterwards.

As for the "not supported". Is it Android or iOS?

It looks like on Android we check for:

locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

But maybe that's not enough and we need to check more.

On iOS we check CLLocationManager.locationServicesEnabled() which I believe is the only way to check it.

I suppose I could also add a flag that bypasses the isAvailable() check, because theoretically its not required to use location services. It would just cause the location functions to throw or return null values.

@jordond
Copy link
Owner

jordond commented Jan 24, 2025

I've changed up the approach on detecting whether or not the device has location services enabled on Android. This is apparently more reliable and specific.

However I have also given the option to disable the check for location services in #172

It will be released in 2.0.0, which as long as the publish process works this time, should be out today.

@Vaibhav2002
Copy link
Author

Hi @jordond

The "Not supported" issue is only there on Android.

If I understood correctly, do you mean

  • Failed to fetch location: Is caused due to timeout issues, and can be fixed by listening to location updates instead of getting current location, as listening will not have any timeout. But what if l don't receive any location update for a long time, i would still need to add a timeout so it does not suspend indefinitely?

  • Does not support geolocation: Will be fixed by the new change you have added in v2.0.0 ?

  • No location found for the input: Sounds self-explanatory, and I don't think if anything can be done regarding this

  • Failed to request permission: Any, Permission requester not found: Any ideas on this, why this could be happening ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants