-
Notifications
You must be signed in to change notification settings - Fork 575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RealmProvider
should have an option like keepRealmOpen
#6023
Comments
@djgamarra In the near future we will be reimplementing how realm open and closures occur. Currently, when one opens the same realm multiple times, a single call to That being said, the solution you have written would be pretty straightforward to implement, so i'll put it up for discussion within the team. Thanks for the request! |
Perfect, this is exactly the problem i am having right now. Is there a way around .close closing both? If i open the realm with exact same config i want that one to run independent
|
@Acetyld this is exactly what happens when we close a Realm, it closes all the instances of the same Realm. What I finally did was to have two instances, the first is the one that By now, I get it working using diff --git a/node_modules/@realm/react/dist/index.js b/node_modules/@realm/react/dist/index.js
index 11274f5..558c916 100644
--- a/node_modules/@realm/react/dist/index.js
+++ b/node_modules/@realm/react/dist/index.js
@@ -356,6 +356,8 @@ function createUseObject(useRealm) {
updatedRef,
});
originalObjectRef.current = originalObject;
+ primaryKeyRef.current = primaryKey;
+ updatedRef.current = true;
}
return cachedObjectRef.current;
}, [realm, originalObject, primaryKey]);
@@ -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); And I have a function like this to get the instance of the realm: let realm: Realm | null = null;
export const usingRealmSingleton = () => {
if (!realm || realm.isClosed) {
realm = new Realm(realmConfig);
}
return realm;
}; |
Aah i see, and how are u using And yhea i dunno i dont want to keep realm open, for example we have a sync function, we open realm, and close at end. But this causes a whole different problem, bcs realm.write is sync and not async lets say we want to insert 1000 records, it happens sync, so my function continues and closes realm causing the insert to throw a error. Maybe i am thinking the wrong way but we are facing mayor issues in current app, right now we are setting a canceled = true and inside the loop in realm.write we stop the loop, and when u set canceled to true we wait 5 seconds to make sure anything outgoing in realm is cancled so we dont get a "trying to access realm.." For now our solution is in foreground jobs to just pass the realm from useRealm to the function (redux dispatch), but later on we are also gonna build a backend worker, my idea was to use the Expo TaskManager |
First, I'm using the Second, the problem you are facing with Realm transactions is something like this?: const realm = new Realm(config);
realm.write(() => {
getData().then(data => {
data.forEach((row) => {
realm.create('Model', row);
});
});
});
realm.close(); |
Uhm, i tested it and looks like the issue is still present <RealmProvider {...realmConfig} closeOnUnmount={false}> We do above in our app globally, we use this for reactive reading etc.. In our sync function we do const realm = await Realm.open(realmConfig);
realm.write(() => {
realm.create(
DepartmentName,
Department.add(realm, item),
UpdateMode.Modified,
);
})
!realm?.isClosed && realm.close(); But the monent we call realm.close() it also closes the one in realmprovider, its so unique and weird, i dont get it. |
Any updates on above? |
@Acetyld If you close realm outside of the RealmProvider, then it will also close the Realm in the provider. If you don't want it to be closed there, then don't close it here. |
@djgamarra and me if i say correct had a different issue that is highly related to this one. Is there a possiblty to look into this? @takameyer |
@Acetyld How about not closing it in the background worker? |
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 errorCannot access a realm that is closed
. I could not find a way to keep my realm instances independent, so that if theRealmProvider
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 innode_modules/@realm/react/dist/index.js
: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
The text was updated successfully, but these errors were encountered: