admin管理员组文章数量:1025518
there is documentation available on how to municate from RN javascript parts with native parts in android and ios over the RN bridge. What is kind of unclear is the lifecycle for bridge munication.
(source medium)
I have a background service in android and need to send events to my RN application. This is how I send events from my android Service
:
private RCTDeviceEventEmitter emitter() {
return mReactContext.getJSModule(RCTDeviceEventEmitter.class);
}
private void pushPayload(String event, WritableMap payload) {
emitter().emit(event, payload);
}
public void sendTimerEvent(long remaining) {
WritableMap data = Arguments.createMap();
data.putInt("remaining", (int) remaining);
pushPayload("timeoutEvent", data);
}
On the javascript side, I use the following static code (not in a ponent, just code imported to my index.android.js
file:
const subscribeForNativeEvents = (eventID, callback) => {
const Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
Emitter.addListener(eventID, callback);
};
subscribeForNativeEvents('timeoutEvent', (event) => {
// work with event.remaining
});
My service runs even when the app is in the background. But I want to make sure that I only try to municate with the javascript side when it is safe to do so.
I sometimes get errors such as React: Calling JS function after bridge has been destroyed
. Is there a way to find out on the native (android) side wether the bridge is available or not?
This is not about how to keep background code alive but rather about to make sure that as long as my native (android) code is running, I can ensure that events are not lost.
there is documentation available on how to municate from RN javascript parts with native parts in android and ios over the RN bridge. What is kind of unclear is the lifecycle for bridge munication.
(source medium)
I have a background service in android and need to send events to my RN application. This is how I send events from my android Service
:
private RCTDeviceEventEmitter emitter() {
return mReactContext.getJSModule(RCTDeviceEventEmitter.class);
}
private void pushPayload(String event, WritableMap payload) {
emitter().emit(event, payload);
}
public void sendTimerEvent(long remaining) {
WritableMap data = Arguments.createMap();
data.putInt("remaining", (int) remaining);
pushPayload("timeoutEvent", data);
}
On the javascript side, I use the following static code (not in a ponent, just code imported to my index.android.js
file:
const subscribeForNativeEvents = (eventID, callback) => {
const Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
Emitter.addListener(eventID, callback);
};
subscribeForNativeEvents('timeoutEvent', (event) => {
// work with event.remaining
});
My service runs even when the app is in the background. But I want to make sure that I only try to municate with the javascript side when it is safe to do so.
I sometimes get errors such as React: Calling JS function after bridge has been destroyed
. Is there a way to find out on the native (android) side wether the bridge is available or not?
This is not about how to keep background code alive but rather about to make sure that as long as my native (android) code is running, I can ensure that events are not lost.
Share Improve this question edited Mar 29, 2018 at 12:45 oliver asked Mar 21, 2018 at 15:52 oliveroliver 9,4954 gold badges36 silver badges39 bronze badges 5- 1 You need to add some detail and code. This is a very broad question as the rules around background services are very plex for both iOS and Android. Especially so if you are targeting Android O and above. You are going to have to narrow this down a lot. – Michael Cheng Commented Mar 21, 2018 at 16:29
- @MichaelCheng Is it really that broad? I use events as a mechanism to municate and try to find a way make sure events are not missed. I added the relevant parts of my code though. – oliver Commented Mar 21, 2018 at 18:56
- The broad part is how your background service is coded. iOS and Android have very strict rules about how long a background service can be kept alive, what permissions must be granted to keep them alive, and even then, they can be killed if deemed necessary by the scheduler. To plicate this, these rules and how this is supposed to be done changes depending on what range of OS versions you plan to support. Then to top that off, there are ways to restart your app once killed. How you work around all of these use cases is why I asked to see code. (...continued in next ment) – Michael Cheng Commented Mar 21, 2018 at 19:16
- 1 Now that you've shown some code, I can see that you're just doing a basic callback and am assuming that your service isn't doing anything fancy to keep itself alive or restarting the app. So there's a good chance that your background service is going to get killed as well. Without knowing what use cases you are trying to cover for, it's hard to give a good remendation. For example, you can try to code it to keep your app alive, or you can assume it will die and store those events in a queue that can be saved to disk that is processed instead of fired immediately, etc, etc. – Michael Cheng Commented Mar 21, 2018 at 19:22
- thanks @MichaelCheng. my problem is not so much about keeping my service alive. It's rather how to make sure that events that I send from android actually can be sent over the bridge. I just want to make sure that as long as my service runs, I don't loose events and only use the bridge if it is available. – oliver Commented Mar 21, 2018 at 19:39
1 Answer
Reset to default 3The issue occurs when the corresponding ReactContext/CatalystInstance has already been destroyed, and you are trying to do some JS stuff from native side (like sending events, doing callbacks etc).
I guess your best bet is to use lifecycle callbacks, i.e. onHostResume, onHostDestroy (https://facebook.github.io/react-native/docs/native-modules-android.html#listening-to-lifecycle-events) or use another callback onCatalystInstanceDestroy(https://github./facebook/react-native/blob/26684cf3adf4094eb6c405d345a75bf8c7c0bf88/ReactAndroid/src/main/java//facebook/react/bridge/NativeModule.java#L51)
Send events only when you know it is not destroyed.
The core idea is that usually the react context is bound to a given Activity/Context in your app, and you can know that lifecycle via callbacks in NativeModule.
You could find more details probably at (https://github./facebook/react-native/blob/b531612b2c917e1f2bd6bb37bf854fe51e658b36/ReactAndroid/src/main/java//facebook/react/ReactInstanceManager.java#L104)
there is documentation available on how to municate from RN javascript parts with native parts in android and ios over the RN bridge. What is kind of unclear is the lifecycle for bridge munication.
(source medium)
I have a background service in android and need to send events to my RN application. This is how I send events from my android Service
:
private RCTDeviceEventEmitter emitter() {
return mReactContext.getJSModule(RCTDeviceEventEmitter.class);
}
private void pushPayload(String event, WritableMap payload) {
emitter().emit(event, payload);
}
public void sendTimerEvent(long remaining) {
WritableMap data = Arguments.createMap();
data.putInt("remaining", (int) remaining);
pushPayload("timeoutEvent", data);
}
On the javascript side, I use the following static code (not in a ponent, just code imported to my index.android.js
file:
const subscribeForNativeEvents = (eventID, callback) => {
const Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
Emitter.addListener(eventID, callback);
};
subscribeForNativeEvents('timeoutEvent', (event) => {
// work with event.remaining
});
My service runs even when the app is in the background. But I want to make sure that I only try to municate with the javascript side when it is safe to do so.
I sometimes get errors such as React: Calling JS function after bridge has been destroyed
. Is there a way to find out on the native (android) side wether the bridge is available or not?
This is not about how to keep background code alive but rather about to make sure that as long as my native (android) code is running, I can ensure that events are not lost.
there is documentation available on how to municate from RN javascript parts with native parts in android and ios over the RN bridge. What is kind of unclear is the lifecycle for bridge munication.
(source medium)
I have a background service in android and need to send events to my RN application. This is how I send events from my android Service
:
private RCTDeviceEventEmitter emitter() {
return mReactContext.getJSModule(RCTDeviceEventEmitter.class);
}
private void pushPayload(String event, WritableMap payload) {
emitter().emit(event, payload);
}
public void sendTimerEvent(long remaining) {
WritableMap data = Arguments.createMap();
data.putInt("remaining", (int) remaining);
pushPayload("timeoutEvent", data);
}
On the javascript side, I use the following static code (not in a ponent, just code imported to my index.android.js
file:
const subscribeForNativeEvents = (eventID, callback) => {
const Emitter = Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter;
Emitter.addListener(eventID, callback);
};
subscribeForNativeEvents('timeoutEvent', (event) => {
// work with event.remaining
});
My service runs even when the app is in the background. But I want to make sure that I only try to municate with the javascript side when it is safe to do so.
I sometimes get errors such as React: Calling JS function after bridge has been destroyed
. Is there a way to find out on the native (android) side wether the bridge is available or not?
This is not about how to keep background code alive but rather about to make sure that as long as my native (android) code is running, I can ensure that events are not lost.
Share Improve this question edited Mar 29, 2018 at 12:45 oliver asked Mar 21, 2018 at 15:52 oliveroliver 9,4954 gold badges36 silver badges39 bronze badges 5- 1 You need to add some detail and code. This is a very broad question as the rules around background services are very plex for both iOS and Android. Especially so if you are targeting Android O and above. You are going to have to narrow this down a lot. – Michael Cheng Commented Mar 21, 2018 at 16:29
- @MichaelCheng Is it really that broad? I use events as a mechanism to municate and try to find a way make sure events are not missed. I added the relevant parts of my code though. – oliver Commented Mar 21, 2018 at 18:56
- The broad part is how your background service is coded. iOS and Android have very strict rules about how long a background service can be kept alive, what permissions must be granted to keep them alive, and even then, they can be killed if deemed necessary by the scheduler. To plicate this, these rules and how this is supposed to be done changes depending on what range of OS versions you plan to support. Then to top that off, there are ways to restart your app once killed. How you work around all of these use cases is why I asked to see code. (...continued in next ment) – Michael Cheng Commented Mar 21, 2018 at 19:16
- 1 Now that you've shown some code, I can see that you're just doing a basic callback and am assuming that your service isn't doing anything fancy to keep itself alive or restarting the app. So there's a good chance that your background service is going to get killed as well. Without knowing what use cases you are trying to cover for, it's hard to give a good remendation. For example, you can try to code it to keep your app alive, or you can assume it will die and store those events in a queue that can be saved to disk that is processed instead of fired immediately, etc, etc. – Michael Cheng Commented Mar 21, 2018 at 19:22
- thanks @MichaelCheng. my problem is not so much about keeping my service alive. It's rather how to make sure that events that I send from android actually can be sent over the bridge. I just want to make sure that as long as my service runs, I don't loose events and only use the bridge if it is available. – oliver Commented Mar 21, 2018 at 19:39
1 Answer
Reset to default 3The issue occurs when the corresponding ReactContext/CatalystInstance has already been destroyed, and you are trying to do some JS stuff from native side (like sending events, doing callbacks etc).
I guess your best bet is to use lifecycle callbacks, i.e. onHostResume, onHostDestroy (https://facebook.github.io/react-native/docs/native-modules-android.html#listening-to-lifecycle-events) or use another callback onCatalystInstanceDestroy(https://github./facebook/react-native/blob/26684cf3adf4094eb6c405d345a75bf8c7c0bf88/ReactAndroid/src/main/java//facebook/react/bridge/NativeModule.java#L51)
Send events only when you know it is not destroyed.
The core idea is that usually the react context is bound to a given Activity/Context in your app, and you can know that lifecycle via callbacks in NativeModule.
You could find more details probably at (https://github./facebook/react-native/blob/b531612b2c917e1f2bd6bb37bf854fe51e658b36/ReactAndroid/src/main/java//facebook/react/ReactInstanceManager.java#L104)
本文标签: javaReact Native sending events from android to javascriptStack Overflow
版权声明:本文标题:java - React Native: sending events from android to javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745629649a2160082.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论