question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EventEmitter is not working in Release apk

See original GitHub issue

Description

I’m using the react native’s event emitter to send events from native to JS. This thing is working absolutely fine on my DEBUG version. I’m able to send events with data from native side to JS, but this is not working in the final RELEASE APK!

To further test out things, I changed my build variant to “release” version. To my surprise it works fine even in release variant. The problem is I’m unable to receive native events in the final release build APK! (gradlew assmbleRelease)

React Native version:

System: OS: Windows 10 10.0.19041 CPU: (8) x64 Intel® Core™ i5-8250U CPU @ 1.60GHz Memory: 450.34 MB / 7.86 GB Binaries: Node: 10.16.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD Watchman: Not Found SDKs: Android SDK: Not Found IDEs: Android Studio: Version 4.0.0.0 AI-193.6911.18.40.6626763 Languages: Java: 13.0.2 Python: 3.8.0 npmPackages: @react-native-community/cli: Not Found react: 16.11.0 => 16.11.0 react-native: 0.62.2 => 0.62.2 npmGlobalPackages: react-native: Not Found

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. Send an event with some data from the native side using event emitter.
  2. Add listener in JS side and try to fetch the results from this native event.
  3. Works fine in DEBUG and RELEASE build variants, but now try generating an APK(RELEASE)

We don’t get events data in release APK!

Expected Results

I’m expecting that my listener at the JS side should listen to the event and get data!

Snack, code example, screenshot, or link to a repository:

In Java (Native) side:

SomeActivity.java (onCreate Method)

Basically, I’m trying to send a JSON object from the native side to JS which I’ve already converted to a NativeMap. Also, the reactContext was coming as null in onCreate method, so I added this check to make sure it’s not null.

if(getIntent().hasExtra("pendingNoonReportDoc")) {
            ReactInstanceManager reactInstanceManager = getReactNativeHost().getReactInstanceManager();
            ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
            if (reactContext != null) {
                reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                        .emit("hasPendingDoc", pendingNoonReportWritableMap);

            } else {
                reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
                    @Override
                    public void onReactContextInitialized(ReactContext context) {
                        context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                                .emit("hasPendingDoc", pendingNoonReportWritableMap);
                        reactInstanceManager.removeReactInstanceEventListener(this);
                    }
                });
            }
        }

In JS-Side

const eventEmitter = new NativeEventEmitter(NativeModules.ClickNativeEg);

const MyApp = () => {
....
	useEffect(() => {
		eventEmitter.addListener("hasPendingDoc", (event) => {
			console.log(event);
			console.log("hasPendingDoc receved event!");
		});
	}, [eventEmitter]); 
....
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

5reactions
benj-spectorycommented, Dec 8, 2020

did anyone manage to find the problem and how to solve it?

1reaction
NikTheGeek1commented, Mar 2, 2022

If you’re on fire, a desperate solution is to add some delay somewhere before you access context, with TimeUnit.SECONDS.sleep(1);. Adding 1 second delay works for me. It’s just a dirty way to resolve the race condition, and to make sure the context is not null.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular EventEmitter not working on some lazy loaded module
I have many lazy-loaded modules and I am using SharedService to send data from HeaderModule to another module's component. But it's working only ......
Read more >
EventEmitter - Angular
Creates an instance of this class that can deliver events synchronously or asynchronously. This class is "final" and should not be extended. See...
Read more >
pyee — pyee 9.0.4 documentation
pyee¶. pyee is a rough port of node.js's EventEmitter. Unlike its namesake, it includes a number of subclasses useful for implementing async and...
Read more >
Angular event handler not working - DevExpress Support
While I can see the Event Emitter for the savedOrderLine event is being called, the orderLineSaved() function is not invoked.
Read more >
Ward Bell: Do Not Expect EventEmitter To Be Observable In ...
Do NOT count on those Observable operators being there in the future! These will be deprecated soon and probably removed before release. Use ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found