Open a Realm - Kotlin SDK
On this page
Open a Realm
Use RealmConfiguration to control the specifics of your realm that you would like to open.
When you open the realm, you must provide a list of object schemas as an argument.
A realm schema is a list of valid object schemas that a realm may contain. Every Realm object must conform to an object type that's included in its realm's schema.
If a realm already contains data when you open it, Realm Database validates each object to ensure that an object schema was provided for its type and that it meets all of the constraints specified in the schema.
Open a Local-Only Realm
To open a realm, create a
RealmConfiguration with
RealmConfiguration.Builder
and pass the resulting RealmConfiguration
to
Realm.open():
val config = RealmConfiguration.Builder(setOf(Frog::class)) .build() val realm = Realm.open(config) Log.v("Successfully opened realm: ${realm.configuration.name}")
Open an In-Memory Realm
To open a realm that runs entirely in memory without being written to a file,
create a RealmConfiguration
using the inMemory
property with RealmConfiguration.Builder.
Then pass the resulting RealmConfiguration
to Realm.open():
val config = RealmConfiguration.Builder(setOf(Frog::class)) .inMemory() .build() val realm = Realm.open(config) Log.v("Successfully opened an in memory realm")
Open a Synced Realm
To open a realm that synchronizes data with Atlas using Device Sync, refer to Open a Synced Realm.
Close a Realm
You can close a realm with realm.close().
The close()
method blocks until all write transactions on the
realm have completed.
realm.close()