Navigation

Open & Close a Local Realm - Android SDK

On this page

  • Open a Local Realm
  • Close a Realm
  • Local Realm Configuration
  • Read-Only Realms
  • In-Memory Realms
  • Dynamic Realms
  • The Default Realm
  • Realm Modules
  • Set the Client Log Level

To open a realm, create a RealmConfiguration with RealmConfiguration.Builder and pass the resulting RealmConfiguration to getInstance() or getInstanceAsync():

Tip
See also:

It is important to remember to call the close() method when done with a realm instance to free resources. Neglecting to close realms can lead to an OutOfMemoryError.

To configure settings for a realm, create a RealmConfiguration with a RealmConfiguration.Builder:

Important
Synchronous Reads and Writes on the UI Thread

By default, you can only read or write to a realm in your application's UI thread using asynchronous transactions. That is, you can only use Realm methods whose name ends with the word Async in the main thread of your Android application unless you explicitly allow the use of synchronous methods.

This restriction exists for the benefit of your application users: performing read and write operations on the UI thread can lead to unresponsive or slow UI interactions, so it's usually best to handle these operations either asynchronously or in a background thread. However, if your application requires the use of synchronous realm reads or writes on the UI thread, you can explicitly allow the use of synchronous methods with the following SyncConfiguration options:

Use the readOnly() method when configuring your realm to make it read-only:

To create a realm that runs entirely in memory without being written to a file, use the inMemory() method when configuring your realm:

To open a Dynamic Realm with a mutable schema, use DynamicRealm:

You can save any RealmConfiguration as the default for your application using the setDefaultConfiguration() method:

You can then use the getDefaultConfiguration() or getDefaultInstance() methods to access that configuration, or an instance created from that configuration:

Realm modules are collections of Realm object models. You can selectively include individual modules when opening a realm.

Note

Libraries that include Realm Database must expose and use their schema through a module. Doing so prevents the library from generating the default RealmModule, which would conflict with the default RealmModule used by any app that includes the library. Apps using the library access library classes through the module.

To configure the log level for Realm logs in your application, pass a LogLevel to RealmLog.setLevel():

Note

Realm uses the log levels defined by Log4J.

Tip

To diagnose and troubleshoot errors while developing your application, set the log level to debug or trace. For production deployments, decrease the log level for improved performance.

Give Feedback

On this page

  • Open a Local Realm
  • Close a Realm
  • Local Realm Configuration
  • Read-Only Realms
  • In-Memory Realms
  • Dynamic Realms
  • The Default Realm
  • Realm Modules
  • Set the Client Log Level