realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Add a prop to stop the Provider from closing the realm (@realm/react)

Open levipro opened this issue 2 years ago • 3 comments

Problem

I have an app that will continue to function while in the background, and on Android specifically there is an issue where the realm react Provider will close the realm when the component tree is unmounted.

This is an issue because other part of the app that will still be functional while in the background still need the realm to be open.

  • On Android when a user swiped the app out of their recent tasks the component tree will unmount causing the Provider to close the realm.
  • Other parts of our app continue to function in the background, many of which access the realm from a singleton class (which was earlier opened directly from realm js)
  • The only way to get our app functional again is to Force stop the app from the android settings so that all our initialization can happen again (I do have a work around to automatically re open the realm when I detect the main component unmounting)
  • This is not ideal and I would greatly appreciate having an option to disable this close via a prop in the realm react Provider

Note: this was not an issue on iOS because swiping closed the app truly did kill it and then the next time the app was started (in foreground or background) all the initialization code runs again from scratch.

Solution

Add a ProviderProps option to disableRealmClose and then on this line look to that prop and determine if the realm should be closed or not.

https://github.com/realm/realm-js/blob/c3464b9dbbc14db341a98143c942546944fe9ea6/packages/realm-react/src/RealmProvider.tsx#L134

Alternatives

I have a work around to re-open the realm when an unmount is detected, but I would be much happier to not have to do that.

How important is this improvement for you?

Dealbreaker

Feature would mainly be used with

Local Database only

levipro avatar Mar 14 '23 22:03 levipro

We have plans in the near future to provide a reference counter for realm.open which will not actually close Realm until all open instances have called realm.close. This would allow you to have an open instance of realm outside of the RealmProvider that doesn't get closed on unmount. I believe that would fix your problem. Thoughts?

takameyer avatar Mar 15 '23 12:03 takameyer

Yes that sounds like a good option, and it would solve my problem. Any idea on when this would be added?

levipro avatar Mar 15 '23 15:03 levipro

We are currently working on v12 of realm which will replace a lot of our c++ code that binds to realm-core with TypeScript. Once this is released, it will enable us to add new features faster and not have to write it in two languages.

takameyer avatar Mar 16 '23 14:03 takameyer