Home > Mobile >  Cannot invoke observeForever on a background thread in React-Native
Cannot invoke observeForever on a background thread in React-Native

Time:04-13

We are trying to integrate a native android framework in react-native using a .aar file. All the steps leading to the integration work fine, i.e. the .aar file gets successfully integrated into the project and the methods are visible throughout the project. We have completed the steps present at - https://reactnative.dev/docs/native-modules-android

However when we are trying to run some methods that are a part of the .aar framework, we run into an error -

Error: Exception in HostFunction: java.lang.IllegalStateException: Cannot invoke observeForever on a background thread

The code implementation looks like this-

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("[email protected]", 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));

   }
}

Even though the function listProfiles is running on the main thread, an error is thrown for

Cannot invoke observeForever on a background thread

What can be the reasons for this behaviour and how can we rectify this.

CodePudding user response:

You return null for Runnables, that could be the reason. You can try to return Runnable and override run method of it.

 private Runnable initializeSDK(String name) {
    return new Runnable() {
        @Override
        public void run() {
            // Your code
        }
    };
}
  • Related