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

时间:2019-03-06 18:48:03

标签: java android react-native

在我的本机JS代码中,我正在调用本机模块,它阻塞了UI线程约1.5秒。使用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);
    }
  }

1 个答案:

答案 0 :(得分:0)

要解决当前的问题,我将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);
    }

尽管我仍然对Java回调模式的解决方案提出质疑。