Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-2489

`RealmProvider` should have an option like `keepRealmOpen`

      Problem

      Hi!

      I apologize if this can be achieved with the current options that RealmProvider has, but I could not find how to make my use case to work with it. Please let me know if there is a way to get it working.

      I'm using:
      React Native 0.72.3
      Realm: 11.10.1
      @realm/react 0.5.1

      This is my problem:

      I have two realms in my app, one is public, the other is private (requires a logged user), none of them uses sync. So, I'm using createRealmContext. I have some RN headless tasks that will use both realms, these tasks can run on background or foreground, but when I tried to use the realms sometimes I was getting the error Cannot access a realm that is closed. I could not find a way to keep my realm instances independent, so that if the RealmProvider closes it the one being used by the headless task keeps open. What I had to do was to patch-package. This is what I changed in node_modules/@realm/react/dist/index.js:

      Unable to find source-code formatter for language: patch. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      --- a/node_modules/@realm/react/dist/index.js
      +++ b/node_modules/@realm/react/dist/index.js
      @@ -665,26 +667,6 @@ function createRealmProvider(realmConfig, RealmContext) {
                       realmRef.current = realm;
                   }
               }, [realm]);
      -        useEffect(() => {
      -            const realmRef = currentRealm.current;
      -            // Check if we currently have an open Realm. If we do not (i.e. it is the first
      -            // render, or the Realm has been closed due to a config change), then we
      -            // need to open a new Realm.
      -            const shouldInitRealm = realmRef === null;
      -            const initRealm = async () => {
      -                const openRealm = await Realm.open(configuration.current);
      -                setRealm(openRealm);
      -            };
      -            if (shouldInitRealm) {
      -                initRealm().catch(console.error);
      -            }
      -            return () => {
      -                if (realm) {
      -                    realm.close();
      -                    setRealm(null);
      -                }
      -            };
      -        }, [configVersion, realm, setRealm]);
               if (!realm) {
                   if (typeof Fallback === "function") {
                       return React.createElement(Fallback, null);
      

      NOTE: This works for me because I'm not changing the Realm config on the fly. Also, I think it can lead to memory leaks.

      Solution

      One solution can be to let the instances independent, but with a prop in the RealmProvider that prevents the realm from being closed.

      Alternatives

      Another solution can be to let us pass to the createRealmContext the realm instance so that we can have a unique instance of the realm in the entire app, for headless tasks or just code outside of the React component tree and for the hooks provided.

      How important is this improvement for you?

      Would be a major improvement

      Feature would mainly be used with

      Local Database only

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: