【问题标题】:React Native - Bridge Android Native to React Native get Slow PerformanceReact Native - 将 Android Native 桥接到 React Native 会降低性能
【发布时间】:2023-04-11 09:39:02
【问题描述】:

我是在将 Native (Android) 桥接到 React Native 方面的新手。

我想从 Android Native 向 React Native 发送大量数据,但遇到性能问题(慢)。

这是我的示例代码:

Android Native(使用回调):

for(int i = 0 ; i < 10000000000000 ; i++){    
this.dispatchEvent("MyEvent","hello this is my message");
}

反应原生:

var myFunction = (tags) => {
            console.log("Message :"+tags);
}

RNTry.on("MyEvent", myFunction);

我得到什么结果:

  1. 来自 Android Native 的消息发送到 React Native 并显示成功结果
  2. React Native UI 冻结,当我触摸按钮时没有响应

我的问题:

如何将大量数据(实时)从 Android Native 发送到 React 没有得到 React Native UI 的 Native 冻结/滞后?

【问题讨论】:

  • 尝试在主线程之外运行它,使用处理程序或异步任务。但是在当前架构下,在事件中发送大量数据显然很慢,使用 turbo 模块应该会更好。
  • @anthonywillismuñoz:turbo 模块是什么意思?
  • 我已经使用异步任务,Android Native 很好,但是几乎实时接收消息的 React Native 在 UI 中变慢了。任何想法 ? @anthonywillismuñoz
  • 没有,你试过只用 100 吗?如果你得到相同的结果?还记得关闭调试器等它滞后 javascript

标签: java android react-native


【解决方案1】:

尝试从 android 发送事件以做出反应

private void sendEvent(ReactContext reactContext,
                      String eventName,
                      @Nullable WritableMap params) {
 reactContext
     .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
     .emit(eventName, params);
}

阅读它 https://reactnative.dev/docs/native-modules-android

【讨论】:

    猜你喜欢
    • 2021-01-12
    • 1970-01-01
    • 2019-01-17
    • 2020-12-03
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    相关资源
    最近更新 更多