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

[Android] Error on setup: "Method addObserver must be called on the main thread" #255

Closed
cristeahub opened this issue Jan 24, 2021 · 6 comments · Fixed by #256
Closed

[Android] Error on setup: "Method addObserver must be called on the main thread" #255

cristeahub opened this issue Jan 24, 2021 · 6 comments · Fixed by #256

Comments

@cristeahub
Copy link

cristeahub commented Jan 24, 2021

Hi,

After upgrading one of my other dependencies in my android app (namely Stripe Android SDK from 10.x to 16.x) I surprisingly encountered an error in this SDK. Note that one of the changes from this huge jump is that the StripeSDK has migrated to AndroidX.

When i do analytics.setup(MY_WRITE_KEY, {some: 'options'} I get a redbox in my app with the text writeKey must not be null or empty.

I have debugged and verified that the writeKey is indeed set correctly, so this is just a symptom and not the actual issue.

Digging further down I found that when initializing the Segment Android SDK I got the following error Method addObserver must be called on the main thread. This seems to be the actual issue causing my subsequent calls to analytics to fail due to the writeKey not being set.

The error is caught here:

} catch(e2: IllegalStateException) {
// pass if the error is due to calling setSingletonInstance multiple times
// if you created singleton in native code already,
// you need to promise.resolve for RN to properly operate

This error comes from the analytics-android package that this package depends on. The error seems to stem from AndroidX issues, and there's even an issue on that repo for the error: segmentio/analytics-android#725 .

This is a blocker for us now, I am asking here since we are using react native and there was no answer from the developers at the other repo. Is there a way to escalate this issue so it gets attention and gets fixed?

I found a related issue from a different vendor here: getsentry/sentry-android#520 . This issue was about lifetime handlers, which both the original issue creator on the segment-android repo and myself use. However simply turning that off was not enough to fix the issue.

Alternatively do you know of any workarounds?

Thanks for a great library otherwise!

@cristeahub cristeahub changed the title Error on setup: "Method addObserver must be called on the main thread" [Android] Error on setup: "Method addObserver must be called on the main thread" Jan 24, 2021
@wmonecke
Copy link

@cristeahub Hi there! I am currently facing the same issue with another RN library.
Did you manage to solve your issue? :)

@cristeahub
Copy link
Author

@cristeahub Hi there! I am currently facing the same issue with another RN library.
Did you manage to solve your issue? :)

There's a chain of issues here, but #256 fixed the issue for me.

You can read more on this issue: segmentio/analytics-android#725 and I used this patch here at one point to fix it: segmentio/analytics-android#725 (comment) .

@wmonecke
Copy link

wmonecke commented Apr 24, 2021

The patch says we should call builder.experimentalUseNewLifecycleMethods(false).

However builder is created like this:

val builder = Analytics
                .Builder(reactApplicationContext, writeKey)
                .flushQueueSize(options.getInt("flushAt"))

In the library that is causing the issue for me (react-native-location) there is no builder object.
This is why I am so confused as to where to call experimentalUseNewLifecycleMethods(false).

I am very inexperienced with Java/Kotlin, sorry if this does not make much sense.

@cristeahub
Copy link
Author

The patch says we should call builder.experimentalUseNewLifecycleMethods(false).

However builder is created like this:

val builder = Analytics
                .Builder(reactApplicationContext, writeKey)
                .flushQueueSize(options.getInt("flushAt"))

In the library that is causing the issue for me (react-native-location) there is no builder object.
This is why I am so confused as to where to call experimentalUseNewLifecycleMethods(false).

I am very inexperienced with Java/Kotlin, sorry if this does not make much sense.

You'll have to take that up with the library authors. I'm not familiar with that library.

@wmonecke
Copy link

wmonecke commented Apr 24, 2021

I have migrated to another library since react-native-location seems abandoned and the authors are not addressing issues or requests to transfer the library to other community members. The other library solved the issue.

Thank you for your help anyways!

@prayansh
Copy link
Contributor

prayansh commented May 5, 2021

Hi All! just wanted to call out that we have shipped the change to fix the main issue in analytics-react-native v1.4.4

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

Successfully merging a pull request may close this issue.

3 participants