无法在 React-Native 的后台线程上调用 observeForever
Cannot invoke observeForever on a background thread in React-Native
我们正在尝试使用 .aar 文件将本机 android 框架集成到 react-native 中。导致集成的所有步骤都运行良好,即 .aar 文件已成功集成到项目中,并且方法在整个项目中都是可见的。
我们已经完成了目前的步骤 - https://reactnative.dev/docs/native-modules-android
然而,当我们尝试 运行 一些属于 .aar 框架的方法时,我们 运行 陷入错误 -
Error: Exception in HostFunction: java.lang.IllegalStateException: Cannot invoke observeForever on a background thread
代码实现是这样的-
package com.frameworksample;
import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.companyName.sdk.Environment;
import com.companyName.sdk.FrameworkKit;
public class CompanyModule extends ReactContextBaseJavaModule {
CompanyModule(ReactApplicationContext context) {
super(context);
}
@NonNull
@Override
public String getName() {
return "CompanyModule";
}
@ReactMethod
public void logMessage(String name, String message) {
Log.d("Company Module", "Logged a message with the name " + name
+ " and message: " + message);
}
public Runnable initializeSDK(String paramOne, String paramTwo) {
TokenProvider tokenProvider = new TokenProvider();
FrameworkKit.initialise(
this.getReactApplicationContext(),
paramOne,
paramTwo,
tokenProvider,
response -> {
listProfiles();
Log.d("Framework Kit Object", "Object initialized successfully" + response);
return null;
}
);
return null;
}
public Runnable listProfiles() {
Log.d("List profile thread", "" + Thread.currentThread());
FrameworkKit.getInstance().listProfiles("employee@comanpany.com", response -> {
Log.d("List Profiles", "Response - " + response);
return null;
});
return null;
}
@ReactMethod(isBlockingSynchronousMethod = true)
public void initCompanyKit(String paramOne, String paramTwo){
Log.d("init method thread", "" + Thread.currentThread());
runOnUiThread(initializeSDK(paramOne, paramTwo));
}
}
即使函数 listProfiles 运行ning 在主线程上,也会为
抛出错误
Cannot invoke observeForever on a background thread
造成这种行为的原因是什么,我们该如何纠正。
你 return null
Runnable
s,这可能是原因。您可以尝试 return Runnable
并覆盖它的 run
方法。
private Runnable initializeSDK(String name) {
return new Runnable() {
@Override
public void run() {
// Your code
}
};
}
我们正在尝试使用 .aar 文件将本机 android 框架集成到 react-native 中。导致集成的所有步骤都运行良好,即 .aar 文件已成功集成到项目中,并且方法在整个项目中都是可见的。 我们已经完成了目前的步骤 - https://reactnative.dev/docs/native-modules-android
然而,当我们尝试 运行 一些属于 .aar 框架的方法时,我们 运行 陷入错误 -
Error: Exception in HostFunction: java.lang.IllegalStateException: Cannot invoke observeForever on a background thread
代码实现是这样的-
package com.frameworksample;
import static com.facebook.react.bridge.UiThreadUtil.runOnUiThread;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.companyName.sdk.Environment;
import com.companyName.sdk.FrameworkKit;
public class CompanyModule extends ReactContextBaseJavaModule {
CompanyModule(ReactApplicationContext context) {
super(context);
}
@NonNull
@Override
public String getName() {
return "CompanyModule";
}
@ReactMethod
public void logMessage(String name, String message) {
Log.d("Company Module", "Logged a message with the name " + name
+ " and message: " + message);
}
public Runnable initializeSDK(String paramOne, String paramTwo) {
TokenProvider tokenProvider = new TokenProvider();
FrameworkKit.initialise(
this.getReactApplicationContext(),
paramOne,
paramTwo,
tokenProvider,
response -> {
listProfiles();
Log.d("Framework Kit Object", "Object initialized successfully" + response);
return null;
}
);
return null;
}
public Runnable listProfiles() {
Log.d("List profile thread", "" + Thread.currentThread());
FrameworkKit.getInstance().listProfiles("employee@comanpany.com", response -> {
Log.d("List Profiles", "Response - " + response);
return null;
});
return null;
}
@ReactMethod(isBlockingSynchronousMethod = true)
public void initCompanyKit(String paramOne, String paramTwo){
Log.d("init method thread", "" + Thread.currentThread());
runOnUiThread(initializeSDK(paramOne, paramTwo));
}
}
即使函数 listProfiles 运行ning 在主线程上,也会为
抛出错误Cannot invoke observeForever on a background thread
造成这种行为的原因是什么,我们该如何纠正。
你 return null
Runnable
s,这可能是原因。您可以尝试 return Runnable
并覆盖它的 run
方法。
private Runnable initializeSDK(String name) {
return new Runnable() {
@Override
public void run() {
// Your code
}
};
}