0

I implemented a native module which navigates to a camera activity. But while calling the camera from nativeModules, it takes 1500-2000ms to open(excluding ui updation in camera).

The native module class that extends ReactContextBaseJavaModule is written like this:

@ReactModule(name = ImagePickerModule.NAME)
public class ImagePickerModule extends ReactContextBaseJavaModule
        implements ActivityEventListener
{
   ....
   // Calling this method
   @ReactMethod
   public void launchCamera(final ReadableMap options, final Callback callback) {
      // this method opens camera after checking necessary permissions and applies UI changes which was set by the user previously
   }
   ....
}

And this is nativeModules to JS mapping in the .ts file

import {NativeModules} from 'react-native';
import {ImagePickerNativeModule} from './privateTypes';

const NativeInterface: ImagePickerNativeModule | undefined =
  NativeModules.ImagePickerManager;

const DEFAULT_OPTIONS: ImagePickerOptions = {
   // options here
}

class ImagePicker {
  launchCamera(options: ImagePickerOptions, callback: Callback): void {
    return NativeInterface.launchCamera(
      {
        ...DEFAULT_OPTIONS,
        ...options,
        tintColor: processColor(options.tintColor || DEFAULT_OPTIONS.tintColor),
      },
      callback,
    );
  }
}

I am calling the nativeModule from react-native code like this:

export const CustomCamera = (): Promise<{ uri: string }> => {
    return new Promise((resolve, reject) => {
        ImagePicker.launchCamera(***Camera options passed***)
          .then(res => // Do something here)
          .catch(err => // Do something here)
    }
}

Is there any way I can open the activity faster while calling it from native modules? or keep the activity in the background so that the cameraActivity can load fast while calling it from react-native? Please suggest.

suvodipMondal
  • 656
  • 10
  • 27
  • Hard to say without an example of what you're working with, see https://stackoverflow.com/help/minimal-reproducible-example – b.stevens.photo Sep 10 '21 at 05:07
  • @t_killah added some of the code snippet, hope it will help you understand the problem – suvodipMondal Sep 10 '21 at 06:16
  • Sorry for the late response I'm sure you've figured out something at this point but it's important to know where the disconnect is happening so that you arent optimizing for something that doesn't need to be optimized. I would start with measuring the time it takes between calling your top most js function to launch the camera to the first line of the java function launchCamera. From that you'll now know whether the slow down is between js & native module or between native module & initializing the camera. – b.stevens.photo Oct 08 '21 at 18:37
  • When you have results from the above suggestion please post here. I don't immediately see anything wrong with your js/ts implementation but there's allot going on there. My guess would be that the disconnect is somewhere in the native java, the camera is likely taking a considerable amount of time to initialize. Again please post back here with updates, if the slowdown is in java please update your java launchCamera function to show complete implementation. – b.stevens.photo Oct 08 '21 at 18:41

0 Answers0