Navigation

Open and Close a Realm

To open a synced realm, call Realm.open(). Pass in a Configuration() object, which must include the sync property defining a SyncConfiguration object.

For example, to open a synced realm as the authenticated user "authedUser", using partition "myPartition" and with a configuration that includes 3 schemas, your code might look similar to this:

const config = {
schema: [schemas.TaskSchema, schemas.UserSchema, schemas.ProjectSchema],
sync: {
user: authedUser,
partitionValue: myPartition,
}
};
const realm = await Realm.open(config);

In the above example, the code shows how to open the realm asynchronously by calling realm.open(). You can also open a realm synchronously by passing a Configuration object to a new instance of the Realm object. This works even if the device is offline.

const realm = new Realm(config);
Info With Circle IconCreated with Sketch.Note

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.

To open a local (non-synced) realm, pass a Configuration() object to either Realm.open() or new Realm(). The following example creates a Configuration object with a path property that defines the local file path to store data.

const config = {
schema: [schemas.TaskSchema, schemas.UserSchema, schemas.ProjectSchema],
path: "./myrealm/data"
};
const realm = await Realm.open(config);

It is important to remember to call the close() method when done with a realm instance to avoid memory leaks.

realm.close();
Give Feedback