Closed cristiano-linvix closed 1 year ago
@cristiano-linvix I would like to know how you open the Realm? Are you aware that you should open the Realm in the render process with { sync: true }
and not with a full sync configuration? See also https://github.com/mongodb-university/realm-electron-advanced-quickstart
Hello @kneth, i have a service with this configs.
I use in my app getRealm() into another files.
import into another file
// REALM import getRealm from "~/database";
const realm = await getRealm();
> src/database/index.tsx
import Realm from "realm";
export const RealmAppConfigs = { id: "xxxxxxxxxxx", timeout: 10000, };
export const RealmConfiguration = { schema: [ // schemas here ], schemaVersion: 28, };
window.RealmDatabaseInstance = null;
const getRealmUser = async () => { const RealmApp = new Realm.App(RealmAppConfigs);
if (RealmApp.currentUser) {
return RealmApp.currentUser;
} else {
const RealmCredentials = Realm.Credentials.serverApiKey("xxxxxxxxxxxxx");
return await RealmApp.logIn(RealmCredentials);
}
};
export default async function getRealm() { // dados do usuário do realm const user = await getRealmUser();
// partition value
const partition_value = "partition_value_here";
if (window.RealmDatabaseInstance) {
return window.RealmDatabaseInstance;
}
window.RealmDatabaseInstance = new Realm({
...RealmConfiguration,
sync: { user: user, partitionValue: partition_value },
});
return window.RealmDatabaseInstance;
}
export async function getAsyncRealm() { // dados do usuário do realm const user = await getRealmUser();
// partition value
const partition_value = "partition_value_here";
// retorna a conexão do realm com await
window.RealmDatabaseInstance = await Realm.open({
...RealmConfiguration,
sync: { user: user, partitionValue: partition_value },
});
return window.RealmDatabaseInstance;
}
export const closeRealm = () => { if (window.RealmDatabaseInstance) { if (window.RealmDatabaseInstance.isClosed === false) { window.RealmDatabaseInstance.close(); } } };
Is this not the correct way to use it?
I have a particularity, because I need the user to configure some things that generate the partition value, in an asynchronous function.
But these settings do not exist at the time of app.whenReady().then(async () => {});
This will happen only after going through the installation screen (where it informs the serial license).
Another thing is that when I open the app I force the server sync to locally using the getAsyncRealm function and wait for the system screens to load.
Your getRealm()
and getAsyncRealm()
should only be used in the main process, and for the render processes you will need to open the Realm with sync: true
instead of sync: { user: user, partitionValue: partition_value }
.
How would I go about passing these partition value values there to the main process, since they depend on the rendering process configuration?
Another question, when I open the app, I force a synchronization of recent data from the server (with the getAsyncRealm() function), how would I do that by opening it in the main process?
For the sync, I used as a basis the documentation (https://www.mongodb.com/docs/realm-sdks/js/latest/Realm.html#.open) which says it forces a sync when used.
Open a Realm asynchronously with a promise. If the Realm is synced, it will be fully synchronized before it is available. In the case of query-based sync, config.schema is required. An exception will be thrown if config.schema is not defined.
We know that using sync: true
is a workaround until Realm has full multiprocess support (includind encryption and device sync - and all operating systems).
You open your sync Realm on the main process with the full configuration (partition value, user, etc.), and on the render processes you open with sync: true
. This will open the Realm file as it was local (no device sync) - and the main process will do all the syncing. So the render processes only need to know the path and not partition value and user.
But, using it this way, if I use await Realm.open() does it force sync with the server?
await Realm.open() does it force sync with the server?
The behaviour can controlled by newRealmFileBehavior
and existingRealmFileBehavior
(see the sync configuration
documentation for details).
We know that using
sync: true
is a workaround until Realm has full multiprocess support (includind encryption and device sync - and all operating systems).You open your sync Realm on the main process with the full configuration (partition value, user, etc.), and on the render processes you open with
sync: true
. This will open the Realm file as it was local (no device sync) - and the main process will do all the syncing. So the render processes only need to know the path and not partition value and user.
Hi, I tried using that same config you mentioned, but when I try to create realm with new Realm({ ...config, sync: true })
inside the renderer process, it gives me the error below:
Module not found: Error: Can't resolve 'react-native' in '/Users/.../app/node_modules/realm/lib' Did you mean './react-native'? Requests that should resolve in the current directory need to start with './'. Requests that start with a name are treated as module requests and resolve within module directories (node_modules). If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
.
Opening the realm connection inside main renderer works fine tho, I', only having problem inside the renderer
How frequently does the bug occur?
All the time
Description
Electron app crashes when reload window.
Sentry logs: https://sentry.io/share/issue/e579f95b8a0b44139fde6f703c5eccca/ https://sentry.io/share/issue/7c819948d9a745248effc7a023b6fedc/
Stacktrace & log output
Can you reproduce the bug?
Yes, always
Reproduction Steps
Create a project with CRA (https://www.mongodb.com/docs/realm/sdk/node/integrations/electron-cra/).
Import in the rendering process the realm and configure the database and insert data.
In my case, I use Device Sync in the cloud.
Once connected to device sync, I press f5 or ctrl + r and the process fails.
Version
10.22.0
What SDK flavour are you using?
Atlas Device Sync
Are you using encryption?
No, not using encryption
Platform OS and version(s)
Win11
Build environment
Cocoapods version
No response