Quick Start - Java SDK
On this page
This Guide Does Not Use Device Sync
This guide can help you get started with device-local Realm Database. If your application needs to communicate with a backend App over the network using features like Atlas Device Sync, Realm Functions, or user management, you should follow the Quick Start with Sync guide.
This page contains information to quickly get Realm Database integrated into your app. Before you begin, ensure you have:
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
onCreate() method of an application subclass to
ensure that you only initialize Realm once each time the
Register Your Application Subclass in the Android Manifest
If you create your own
Application subclass, you must add it to your
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
subclass before any other class when a user launches your application.
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mongodb.example"> <application android:name=".MyApplicationSubclass" ... /> </manifest>
Define Your Object Model
Your application's data model defines the structure of data stored within Realm Database. You can define your application's data model via Kotlin or Java classes in your application code with Realm Object Models.
To define your application's data model, add the following class definitions to your application code:
Open a Realm
RealmConfiguration to control the specifics of the realm you
would like to open, including the name or location of the realm,
whether to allow synchronous reads or writes to a realm on the UI
thread, and more.
Create, Read, Update, and Delete Objects
Once you have opened a realm, you can modify the objects within that realm in a write transaction block.
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
To create a new
Task, instantiate an instance of the
Task class and add it to the realm in a write block:
You can retrieve a live collection of all items in the realm:
You can also filter that collection using a filter:
To modify a task, update its properties in a write transaction block:
Finally, you can delete a task by calling the
method in a write transaction block:
Watch for Changes
You can watch a realm, collection, or object for changes by attaching a custom
OrderedRealmCollectionChangeListener with the
If you're running this project in a fresh Android Studio project, you can
copy and paste this file into your application's
MainActivity -- just
use a package declaration at the top of the file for your own project
TaskStatusif you're using java
Running the above code should produce output resembling the following:
Successfully authenticated anonymously. Updated range: 0 to 1 Deleted range: 0 to 1 Successfully logged out.