Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ /

Connect to an Atlas App Services backend - Java SDK

On this page

  • Initialize Realm
  • Access the App Client
  • Configuration

The App client is the interface for the App Services backend. It provides access to the authentication functionality, functions, and sync management.

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); // context, usually an Activity or Application
Realm.init(this) // context, usually an Activity or Application

Tip

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 logic. Set the android.name property of your manifest's application definition to ensure that Android instantiates your Application subclass before any other class when a user launches your application.

<?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>

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

String appID = YOUR_APP_ID; // replace this with your App ID
App app = new App(new AppConfiguration.Builder(appID).build());
val appID : String = YOUR_APP_ID // replace this with your App ID
val app: App = App(AppConfiguration.Builder(appID).build())

Important

Initialize the App before Creating an Instance

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

For most use cases, you only need your application's App ID to connect to App Services. 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 optionally use the AppConfiguration Builder to control details of your App:

String appID = YOUR_APP_ID; // replace this with your App ID
App app = new App(new AppConfiguration.Builder(appID)
.appName("My App")
.requestTimeout(30, TimeUnit.SECONDS)
.build());
val appID = YOUR_APP_ID // replace this with your App ID
val app: App = App(AppConfiguration.Builder(appID)
.appName("My App")
.requestTimeout(30, TimeUnit.SECONDS)
.build())

You can create multiple App client instances to connect to multiple Apps. All App client instances that share the same App ID use the same underlying connection.

Important

Changing an App Config After Initializing the App

Changed in version v10.18.0: urlPrefix is not cached in the App configuration

When you initialize the App client, the configuration is cached internally. Attempting to close and then re-open an App with a changed configuration within the same process has no effect. The client continues to use the cached configuration.

In Java SDK version 10.18.0 and later, the urlPrefix is no longer cached in the App configuration. This means that you can change the urlPrefix, and the App client will use the updated configuration. In earlier SDK versions, changes to the urlPrefix in a cached App configuration have no effect.

Back

Atlas App Services

Next

Call a Function