Docs Menu

Docs HomeRealm

Open a Realm - Kotlin SDK

On this page

  • Open a Realm
  • Open a Local-Only Realm
  • Open an In-Memory Realm
  • Open a Synced Realm
  • Close 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.

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}")

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")

To open a realm that synchronizes data with Atlas using Device Sync, refer to Open a Synced Realm.

You can close a realm with realm.close(). The close() method blocks until all write transactions on the realm have completed.

realm.close()
←  Manage Realm Database Files - Kotlin SDKDelete a Realm - Kotlin SDK →
Share Feedback
© 2023 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.