在我的本机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);
}
}
答案 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回调模式的解决方案提出质疑。