Realm supports Kotlin APIs which make it easier to use Realm mobile database. It is a rich domain-specific language that allows developers the flexibility of expressing their code in Kotlin instead of having to re-implement all standard Java classes (or even theming frameworks like Android).
The “realm kotlin example” is a collection of Kotlin APIs/tools to make using Realm Mobile database easier. It also includes a sample project.
Working with Realm is made simpler using the Kotlin API and tools.
Components
Compass is a set of Kotlin classes and extensions that efficiently manage Realm’s lifecycle and threading model, making it simpler to deal with Realm. It is made up of two main components.
- compass — The Kotlin core API plus a collection of extensions for typical Realm lifecycle and threading patterns.
- compass-paging — Provides Jetpack Paging 3 integration extensions.
- More information will be available shortly. ™
The First Steps
Compass is available on mavenCentral as android library assets. In the root build.gradle file, add the following:
mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() mavenCentral() ma
Alternatively, in the context of dependencies In settings, there is resolution management. gradle:
mavenCentral() dependencyResolutionManagement repositories
Then, in your modules, include the following:
dependencies “dev.arunkumar.compass:compass:1.0.0” / “dev.arunkumar.compass:compass-paging:1.0.0” / “dev.arunkumar.compass:compass:1.0.0”
Setup
Compass expects that Realm.init(this) and Realm.setDefaultConfiguration(config) have previously been invoked and obtains a default instance of Realm using Realm.getDefaultInstance() as necessary.
Features
Construction of a query
To create RealmQuery objects, use the RealmQuery creation function. RealmQuery avoids threading restrictions by postponing invocation to the use site rather than the call site by using lambdas.
where().sort(Person.NAME) val personQueryBuilder = RealmQuery
RealmQueryBuilder provides extensions that take use of this paradigm, such as getAll().
Threading
The constantly updating object model in Realm requires minimal threading restrictions. These are the rules:
- Realms can only be visited from the thread in which they were created.
- Realms can only be viewed from threads that have been prepared using Android’s Looper.
- Threads cannot pass Managed RealmResults around.
Compass attempts to make working with Realm simpler by offering safe defaults.
RealmExecutor/RealmDispatcher
RealmExecutor and RealmDispatcher are supplied, which use HandlerThreads to prepare Android Looper internally. The following statements are correct:
var people = realm.where / Acquire default Realm with ‘Realm’ (). realmChangeListener = RealmChangeListenerRealmResults> findAll() val realmChangeListener = RealmChangeListenerRealmResults> people println(“Change listener called”) realm.transact.addChangeListener(realmChangeListener) / Safe to add / Make a transaction realm.transact.addChangeListener(realmChangeListener) / this: Realm copyToRealm(Person()) delay(500) / Wait until the change listener is triggered / Acquired Realm is closed automatically } withContext(RealmDispatcher()) withContext(RealmDispatcher()) withContext(RealmDispatcher() realm -> realm -> realm -> realm -> realm -> realm -> realm -> realm -> realm var people = realm.where / Acquire default Realm with ‘Realm’ (). realmChangeListener = RealmChangeListenerRealmResults> findAll() val realmChangeListener = RealmChangeListenerRealmResults> people println(“Change listener called”) realm.transact.addChangeListener(realmChangeListener) / Safe to add / Make a transaction realm.transact.addChangeListener(realmChangeListener) / this: Realm copyToRealm(Person()) delay(500) / Wait for the change listener to be triggered / Acquired Realm will be closed automatically
When RealmDispatcher is no longer being utilized to release resources, it should be closed. See the section below for more information on using Flow to handle lifecycles automatically.
Flowing Streams
Compass has enhancements that make it simple to convert queries to Flow and verifies that a Flow follows fundamental threading requirements.
- Objects that are returned may be transferred across threads.
- Flow collection is not halted until the Realm lifetime is completed.
where() val personsFlow = RealmQuery asFlow()
AsFlow builds a separate RealmDispatcher to execute the queries and track changes internally. When collection stops/restarts, the generated dispatcher is automatically closed and rebuilt. Realm is used to copy all RealmResults objects by default. copyFromRealm.
Read just a portion of the data.
Because copying big objects from Realm may be memory intensive, use the asFlow() overload with a transform function to read just a subset of results to memory.
PersonName data class (val name: String) where() val personNames = RealmQuery PersonName(it.name) asFlow
Paging
Compass offers RealmQueryBuilder extensions that allow paging support. Consider the following scenario:
where() val pagedPersons = RealmQuery val pagedPersons = RealmQuery val pagedPersons = RealmQuery val pagedPersons = Realm asPagingItems()
When Flow collection is ended, asPagingItems maintains a Realm instance, runs queries using RealmDispatcher, and cleans up resources.
Use the asPagingItems() overload with a transform function to read just a subset of items into memory:
where() val pagedPersonNames = RealmQuery it.name it.asPagingItems it.asPagingItems it.asPagingItems it.a
ViewModel
ViewModel integration is required.
ViewModel() val results = RealmQuery where() where() where() where() where() where() where() where() where() where() where() where() where() where() where() where() where() where() where() where() where() where() where cachedIn(viewModelScope).asPagingItems()
AsPagingItems() returns a Flow that may be securely used for transformations, separators, and caching. Although it is possible, it is preferable to use asPagingItems /* convert */ to convert to UI model since it is more efficient.
Compose
Compose may use the FlowPagingData> generated by asPagingItems() by using collectAsLazyPagingItems() from paging-compose:
tasks.collectAsLazyPagingItems; val items = tasks.collectAsLazyPagingItems; val items = tasks.collectAs () modifier = modifier.padding(contentPadding),) LazyColumn(modifier = modifier.padding(contentPadding),) task -> taskContent(task) items(items = items, key = task -> task.id.toString() ) task -> taskContent(task)
FAQ
Why don’t you try Realm Freeze?
The canonical method to transfer things across threads is using Frozen Realm objects. However, it still states that it is linked to the underlying Realm and that threading is a danger.
Frozen items are valid for as long as the realm in which they were created is open. Close worlds containing frozen items only when all threads have finished working with them.
The transform API in Compass allows you to create detached objects from Realm as well as read a subset of Realm objects into memory.
See here for a more comprehensive comparison.
Resources
GitHub
https://github.com/arunkumar9t2/compass
The “mongodb realm” is a collection of Kotlin APIs/tools to make using Realm Mobile database easier. The “mongodb realm” is an open source project that was created by the creators of Realm Mobile Database.
Frequently Asked Questions
What is realm Kotlin?
A: Realm is an IRC client.
What type of database is realm?
A: Realm is a NoSQL database that supports JSON and key-value stores.
How does realm database work?
A: The realm database is a collection of all the available songs and items in the game, which are divided into four categories. These include Songs, Items, Levels and Playlists. With each category comes two sub-categories – one for individual song files, and another for playlist files.
Related Tags
- realm studio
- realm kotlin github
- realm database
- realm-android
- realm kotlin android