Open & Close a Realm - React Native SDK
On this page
Overview
Realms are the core data structure used to organize data in Realm Database. At its core, a realm is a collection of the objects that you use in your application, called Realm objects, as well as additional metadata that describe the objects.
This page details how to configure and open a realm using the React Native SDK.
Key Concept: Realm Files
Realm Database stores a binary encoded version of every object and type in a
realm in a single .realm
file. The file is located at a specific path that
you define when you open the realm.
Tip
Implement Compacting in Your Production Application
Every production application should implement a shouldCompactOnLaunch
callback to periodically reduce the realm file size.
Note
Auxiliary Realm Files
Realm Database creates additional files for each realm. To learn more about these files, see Realm Database Internals.
Warning
Use Caution When Deleting Realm Files
In some circumstances, such as a client reset scenario, you might need to delete a realm file and its auxiliary files.
If you delete a realm file or any of its auxiliary files while one or more instances of the realm are open, you might corrupt the realm or disrupt sync.
You may safely delete these files when all instances of a realm are closed. Before you delete a realm file, make sure that you back up any important objects as you will lose all unsynced data in the realm.
Open a Local Realm
To open a local (non-synced) realm, pass a Realm.Configuration object to the asynchronous method Realm.open().
Note
Accessing the Default Realm Path
If the path
property is not specified in your Configuration
object,
the default path is used. You can access and change the default Realm path
using the Realm.defaultPath
global property.
const Car = { name: "Car", properties: { make: "string", model: "string", miles: "int", }, }; // Open a local realm file with a particular path & predefined Car schema try { const realm = await Realm.open({ schema: [Car], }); realm.close(); } catch (err) { console.error("Failed to open the realm", err.message); }
Open an In Memory Realm
Open an In-Memory Realm
To create a realm that runs entirely in memory without being written to a file,
add inMemory: true
to your Realm.Configuration object:
const realm = await Realm.open({ inMemory: true, schema: [Car], });
Note
In-memory realms may use disk space if memory is running low, but files created by an in-memory realm are deleted when you close the realm.
Tip
See also: Additional Ways to Configure a Realm
Learn how to Open a Synced Realm
Learn how to Encrypt a Realm
Learn how to Bundle a Realm
Close a Realm
It is important to remember to call the close()
method when done with a
realm instance to avoid memory leaks.
realm.close();