mrousavy / react-native-vision-camera

📸 A powerful, high-performance React Native Camera library.
https://react-native-vision-camera.com
MIT License
7.28k stars 1.07k forks source link

✨ Add possibility to cleanup resources when camera is being unmounted in frame processor plugin #3152

Open AlexShukel opened 2 weeks ago

AlexShukel commented 2 weeks ago

What feature or enhancement are you suggesting?

Currently, if frame processor plugin registers some listeners in it's constructor (for example, OrientationEventListener or any kind of sensors), there is no way to unregister those listeners when camera component unmounts.

The only way to unregister listeners is using onHostPause and onHostDestroy from LifecycleEventListener, but in that case listeners will drain battery even if user closed camera activity.

What Platforms whould this feature/enhancement affect?

iOS, Android

Alternatives/Workarounds

It is possible to register native module that will be able to cleanup resources, and call the native method from JS in useEffect when camera is being unmounted.

Additional information

maintenance-hans[bot] commented 2 weeks ago

Guten Tag, Hans here! 🍻

Ze request for cleaning up resources when mounting ze camera is a valid enhancement! It seems you’ve identified a potential issue with listeners draining battery after unmounting. Consider adding a cleanup method in your frame processor plugin to unregister listeners, as you mentioned in your alternatives.

If this needs further clarification or goes deeper than expected, feel free to open a discussion on our discussions page. If you want to support ze project and get quicker responses from mrousavy, consider sponsoring mrousavy.

Let’s see how ze community responds!

Note: If you think I made a mistake, please ping @mrousavy to take a look.

mrousavy commented 2 weeks ago

Hm, it's hard to add such lifecycle listeners because the Camera has no connection or knowledge of any of it's plugins.

You can add that code in the deinit/dealloc methods on iOS, and on finalize on Android, but it will only be called once the JS object (instance of the plugin) will actually be destroyed by the garbage collector..

AlexShukel commented 2 weeks ago

@mrousavy I think it's okey to manually cleanup resources from react's useEffect hook in Camera component, but it is also not fully functional workaround.

The problem occurs when users leaves application in background - then it would be nice to cleanup all listeners in order to prevent battery drain. So, if you clean listeners in onHostPause method of com.facebook.react.bridge.LifecycleEventListener, then you should register them in onHostResume. But, you shouldn't register listeners on resume if camera is not mounted. However, it is not possible to determine whether camera is mounted or not from plugin side.

Maybe it is possible to add some kind of lifecycle indicator in VisionCameraProxy?

mrousavy commented 2 weeks ago

Good idea, yes we can add something like addLifecycleListener to VisionCameraProxy!

mrousavy commented 2 weeks ago

Also, Nitro Modules will have proper memory management and can be eagerly released - but that is still not public