简体   繁体   English

React-Native NativeModules-是否有Android / Java回调模式

[英]React-Native NativeModules - Is there an Android/Java callback pattern

In my react-native JS code I'm calling a Native Module and it was blocking the UI thread for about 1.5sec. 在我的本机JS代码中,我正在调用本机模块,它阻塞了UI线程约1.5秒。 Running it on a different thread with a Runnable works but I can't capture the returned value that happens inside the Runnable? 使用Runnable在不同的线程上运行它,但是我无法捕获Runnable内部发生的返回值?

@ReactMethod
  public void print(final String printerAddress, final String price, final String description, final String upc, Promise promise) {
    try {
      boolean success = false;

      new Thread(new Runnable() {
        public void run() {
          success = mEpsonPrinter.printLabel(printerAddress, price, description, upc);
        }
      }).start();

        promise.resolve(success);
    } catch (IllegalViewOperationException e) {
        promise.reject(e);
    }
  }

To resolve the immediate problem I placed the promise.resolve(success) call inside the Runnable.run() 为了解决眼前的问题,我将promise.resolve(success)调用放在Runnable.run()

try {    
      new Thread(new Runnable() {
        public void run() {
          boolean success = mEpsonPrinter.printLabel(printerAddress, price, description, upc);

        promise.resolve(success);
        }
      }).start();
    } catch (IllegalViewOperationException e) {
        promise.reject(e);
    }

Although I'm still left with questioning a solution for callback pattern with Java. 尽管我仍然对Java回调模式的解决方案提出质疑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM