Execute realm operation before realm database closes

Hello,

I would like to do an operation just before the app is closed.
I try to put this operation in the useeffect return function when the component get unmounted but i get the following error :
LOG [Error: Cannot access realm that has been closed.]
e.g :

  useEffect(() => {
    return async () => {
      try {
        realm.write(() => {
              realm.delete(myobject);
            }
        });
      } catch (err) {
        console.log(err);
      }
    };
  }, []);

Any idea how to do it properly ?

Thank you

You could take the closing of Realm in your own hands by adding closeOnUnmount={false} to the RealmProvider props. This will make the RealmProvider skip closing the realm as the app is closed.

Yes i have seen this option this morning so i will try it a bit later but it seems to be what i needed.
I will tag this answer as “solution” if it fixes my issue

Thank you for your answer

Also i have seen a request about adding props to the realmProvider such as onOpen or onClose that would be a nice solution for nice setup and cleanup of realm data :

Best regards,
Cyril

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.