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

🔥 Method addObserver must be called on the main thread #154

Open
wmonecke opened this issue Apr 23, 2021 · 2 comments
Open

🔥 Method addObserver must be called on the main thread #154

wmonecke opened this issue Apr 23, 2021 · 2 comments

Comments

@wmonecke
Copy link

wmonecke commented Apr 23, 2021

Hi! 👏

I have been using this package for a while until I recently stumbled upon this bug (I did not even update this package).
The error (like the title says) is just Method addObserver must be called on the main thread and appears when calling getLatestLocation after getting permission and configuring RNLocation.

I am very inexperienced with Java so it is a bit hard for me to fix this bug. I will try to dig into the code.
If anyone is experiencing the same issue, please let me know if you discover a way to solve it.

I have found similar bugs in other libraries:

  1. Method addObserver must be called on the main thread getsentry/sentry-android#520
  2. [Android] Error on setup: "Method addObserver must be called on the main thread" segmentio/analytics-react-native#255 (Error after upgrading another unrelated library, like in my case)

The error hints to this method in RNPlayServicesLocationProvider.java:

   public RNPlayServicesLocationProvider(Activity activity, ReactApplicationContext context) {
        this.context = context;
        if (activity != null) {
            locationProvider = LocationServices.getFusedLocationProviderClient(activity); // failing here
            locationSettingsClient = LocationServices.getSettingsClient(activity);
        } else {
            locationProvider = LocationServices.getFusedLocationProviderClient(context);
            locationSettingsClient = LocationServices.getSettingsClient(context);
        }
    }

Cheers!

@wmonecke
Copy link
Author

I changed libraries to react-native-geolocation-service

@vivekvardhanadepu
Copy link

using the context rather than activity solved the issue

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