Navigation

Initialize the Realm App Client

Overview

The Realm Realm app client is the interface to the MongoDB Realm backend. It provides access to the authentication functionality, functions, and sync management.

Initialize Realm

Before you can use Realm in your app, you must initialize the Realm library. Your application should initialize Realm just once each time the application runs.

To initialize the Realm library, provide an Android context to the Realm.init() static function. You can provide an Activity, Fragment, or Application context for initialization with no difference in behavior. You can initialize the Realm library in the onCreate() method of an application subclass to ensure that you only initialize Realm once each time the application runs.

Realm.init(this) // `this` is a Context, typically an Application or Activity
Realm.init(this); // `this` is a Context, typically an Application or Activity

Register Your Application Subclass in the Android Manifest

If you create your own Application subclass, you must add it to your application’s AndroidManifest.xml to execute your custom application code. Setting the android.name property of your manifest’s application definition instantiates your Application subclass before any other class when the process for your application is created.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.mongodb.example">

   <application
      android:name=".MyApplicationSubclass"
      ...
   />
</manifest>

Access the App Client

Pass the Realm app ID for your Realm app, which you can find in the Realm UI.

val appID : String = "<your App ID>" // replace this with your App ID
Realm.init(this) // `this` is a Context, typically an Application or Activity
val app: App = App(AppConfiguration.Builder(appID).build())
String appID = "<your App ID>"; // replace this with your App ID
Realm.init(this); // `this` is a Context, typically an Application or Activity
App app = new App(new AppConfiguration.Builder(appID).build());

Initialize the App before Creating an Instance

You must initialize your Realm app connection with Realm.init() before creating any instance of an App.

Note

You can create multiple App instances to connect to multiple Realm apps or to the same Realm app with different configurations. All App instances that share the same App ID use the same underlying connection.

Configuration

For more granular control of the details of your app connection, such as custom timeouts for connections, codecs used for MongoDB Data Access, and keys for local encryption, you can use the AppConfiguration Builder to control details of your App:

val appID = "<your App ID>" // replace this with your App ID
Realm.init(this) // `this` is a Context, typically an Application or Activity
val app: App = App(AppConfiguration.Builder(appID)
                   .appName("My App")
                   .requestTimeout(30, TimeUnit.SECONDS)
                   .build())
String appID = "<your App ID>"; // replace this with your App ID
Realm.init(this); // `this` is a Context, typically an Application or Activity
App app = new App(new AppConfiguration.Builder(appID)
                  .appName("My App")
                  .requestTimeout(30, TimeUnit.SECONDS)
                  .build());

Note

For most use cases, you only need your application’s App ID to connect to MongoDB Realm. The other settings demonstrated here are optional.