Best practice to handle multiple realm

Using :

Node js sdk
electron app
Vue js(renderer)

I have an app in which initially I want to sync data from server to local instance and for that I’m opening realm asynchronously and I’m closing it when done with the syncing (shall I close this ? as I want to react to the collection changes as well).
realm = await Realm.open(config)

Then after the syncing is done I’m quering in my app for some data which I have synced
by opening my realm synchronously (as user can go offline now) by using:
realm = new Realm(config)

my config object

    config  = {
                schema,
                path: getDBPath(),
                sync: {
                  user: app.currentUser,
                  partitionValue: new ObjectID(getCatalogId()),
                  error: (error) => {
                    console.log(error.name, error.message)
                  }
                }
            }

What is the best practice to close the realm and when?

The first time a user logs on to your realm app, you should open the realm asynchronously to sync data from the server to the device. After that initial connection, you can open a realm synchronously to ensure the app works in an offline state. Docs link

So I 'm following the same approach but when opening the same config object with two different methods to open the realm I’m getting the error that "same instance is opened on the current thread " which is solved by closing the instance.

But I want to react to changes in the collection if any how can i do that?

That docs snippet is referring to subsequent launches of the app not within the same app launch. Speaking generally, you’ll want to open the realm on appLaunch and then close it when the app is shutting down. Once the app is open you can observe changes by attaching a change listener to the open realm reference and react to changes.

1 Like

Thanks for the reply. When I open a realm instance on the app launch and not close it, I get an error when I try to open it in some other component using realm = new Realm(config) and error I get is:

realm already opened on current thread with different schema

What should I do for this.?

You should open the realm with the same configuration

1 Like

I’m opening it the wit the same config but using different method.
At launch I use Async open realm = await Realm.open( config) and now if the data is synced so user can go offline and I’m supposed to open it using realm = new Realm(config) right?

Any Update on this ticket as I’m still facing the issue?

@shawn_batra Can you file an issue here with a reproduction case please - GitHub - realm/realm-js: Realm is a mobile database: an alternative to SQLite & key-value stores

I believe this should work as long as you are not changing the configuration.