Collection of Kotlin APIs/tools to make using Realm Mobile database easier

Overview

Compass

Kotlin API and tools to make working with Realm easier

Components

Compass is designed to make working with Realm easier through collection of Kotlin types and extensions that handle Realm's lifecycle and threading model effectively. It has two major components

  • compass - The core Kotlin API with set of extensions for common patterns around Realm's lifecycle and threading.
  • compass-paging - Provides extensions to integrate with Jetpack Paging 3.
  • more coming soon..™

Getting Started

Compass is available as android library artifacts on mavenCentral. In root build.gradle:

allprojects {
  repositories {
    mavenCentral()
  }
}

Or in dependenciesResolutionManagement in settings.gradle:

dependencyResolutionManagement {
  repositories {
    mavenCentral()
  }
}

Then in your modules:

dependencies {
    implementation "dev.arunkumar.compass:compass:1.0.0"
    // Paging integration
    implementation "dev.arunkumar.compass:compass-paging:1.0.0"
}

Setup

Compass assumes Realm.init(this) and Realm.setDefaultConfiguration(config) is called already and acquires a default instance of Realm using Realm.getDefaultInstance() where needed.

Features

Query construction

Use RealmQuery construction function to build RealmQuery instances. Through use of lambdas, RealmQuery{} overcomes threading limitations by deferring invocation to usage site rather than call site.

val personQueryBuilder =  RealmQuery { where<Person>().sort(Person.NAME) }

Extensions like getAll() is provided on RealmQueryBuilder that takes advantage of this pattern.

Threading

Realm's live updating object model mandates few threading rules. Those rules are:

  1. Realms can be accessed only from the thread they were originally created
  2. Realms can be observed only from threads which have Android's Looper prepared on them.
  3. Managed RealmResults can't be passed around the threads.

Compass tries to make it easier to work with Realm by providing safe defaults.

RealmExecutor/RealmDispatcher

RealmExecutor and RealmDispatcher are provided which internally prepares Android Looper by using HandlerThreads. The following is valid:

withContext(RealmDispatcher()) {
    Realm { realm -> // Acquire default Realm with `Realm {}`
        val persons = realm.where<Person>().findAll()

        val realmChangeListener = RealmChangeListener<RealmResults<Person>> {
            println("Change listener called")
        }
        persons.addChangeListener(realmChangeListener) // Safe to add

        // Make a transaction
        realm.transact { // this: Realm
            copyToRealm(Person())
        }
        
        delay(500)  // Wait till change listener is triggered
    } // Acquired Realm automatically closed
}

Note that RealmDispatcher should be closed when no longer used to release resources. For automatic lifecycle handling via Flow, see below.

Streams via Flow

Compass provides extensions for easy conversions of queries to Flow and confirms to basic threading expectations of a Flow

  • Returned objects can be passed to different threads.
  • Handles Realm lifecycle until Flow collection is stopped.
val personsFlow = RealmQuery { where<Person>() }.asFlow()

Internally asFlow creates a dedicated RealmDispatcher to run the queries and observe changes. The created dispatcher is automatically closed and recreated when collection stops/restarted. By default, all RealmResults objects are copied using Realm.copyFromRealm.

Read subset of data.

Copying large objects from Realm can be expensive in terms of memory, to read only subset of results to memory use asFlow() overload that takes a transform function.

data class PersonName(val name: String)

val personNames = RealmQuery { where<Person>() }.asFlow { PersonName(it.name) }

Paging

Compass provides extensions on RealmQueryBuilder to enable paging support. For example:

val pagedPersons = RealmQuery { where<Person>() }.asPagingItems()

asPagingItems internally manages a Realm instance, run queries using RealmDispatcher and cleans up resources when Flow collection is stopped.

For reading only subset of objects into memory, use the asPagingItems() overload with a transform function:

val pagedPersonNames = RealmQuery { where<Person>() }.asPagingItems { it.name }
ViewModel

For integration with ViewModel

class MyViewModel: ViewModel() {

    val results = RealmQuery { where<Task>() }.asPagingItems().cachedIn(viewModelScope)
}

The Flow returned by asPagingItems() can be safely used for transformations, separators and caching. Although supported, for converting to UI model prefer using asPagingItems { /* convert */ } as it is more efficient.

Compose

The Flow<PagingData<T>> produced by asPagingItems() can be consumed by Compose with collectAsLazyPagingItems() from paging-compose:

val items = tasks.collectAsLazyPagingItems()
  LazyColumn(
    modifier = modifier.padding(contentPadding),
  ) {
    items(
      items = items,
      key = { task -> task.id.toString() }
    ) { task -> taskContent(task) }
  }

FAQ

Why not Realm Freeze?

Frozen Realm objects is the official way to safely move objects around threads. However it still says connected to underlying Realm and poses risk around threading.

Frozen objects remain valid for as long as the realm that spawned them stays open. Avoid closing realms that contain frozen objects until all threads are done working with those frozen objects.

Compass's transform API supports both creating detached objects from Realm and reading subset of Realm object into memory.

For detailed comparison, see here.

Resources

You might also like...
SquiDB is a SQLite database library for Android and iOS

Most ongoing development is currently taking place on the dev_4.0 branch. Click here to see the latest changes and try out the 4.0 beta. Introducing S

Compile time processed, annotation driven, no reflection SQLite database layer for Android

SqliteMagic Simple yet powerful SQLite database layer for Android that makes database handling feel like magic. Overview: Simple, intuitive & typesafe

android 数据库框架,sqlite database

DBExecutor 主要的功能 1.使用了读写锁,支持多线程操作数据。 2.支持事务 3.支持ORM 4.缓存Sql,缓存表结构 这个类库主要用于android 数据库操作。 始终围绕着一个类对应一个表的概念。 只要创建一个实体类,就不用当心它怎么存储在数据库中,不用重新写增删改查的代码。基本的功

LiteOrm is a fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line of code efficiently.

#LiteOrm:Android高性能数据库框架 A fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line

An Android library that makes developers use SQLite database extremely easy.

LitePal for Android 中文文档 LitePal is an open source Android library that allows developers to use SQLite database extremely easy. You can finish most o

An app with implementation of Room database for Android platform

Room Room An app with implementation of Room database for Android platform The Room persistence library provides an abstraction layer over SQLite to a

Kotlin-Exposed-SQL - Example of using Exposed with Kotlin for the consumption of relational SQL Databases
Kotlin-Exposed-SQL - Example of using Exposed with Kotlin for the consumption of relational SQL Databases

Kotlin Exposed SQL Sencillo ejemplo sobre el uso y abuso de Exposed ORM de Jetbr

A simple NoSQL client for Android. Meant as a document store using key/value pairs and some rudimentary querying. Useful for avoiding the hassle of SQL code.

SimpleNoSQL A simple NoSQL client for Android. If you ever wanted to just save some data but didn't really want to worry about where it was going to b

To-Do App using Modern Declarative UI Toolkit called Jetpack Compose

Daedalus-Scheduler To-Do App using Modern Declarative UI Toolkit called Jetpack Compose The Brief App that searches recipes from the api spoonacular A

Comments
  • Initial work on `RealmExecutor`

    Initial work on `RealmExecutor`

    Introduce RealmExecutor that can execute realm queries and also observe them. This executor effectively removes Realm's UI thread dependency to observe RealmModels.

    enhancement 
    opened by arunkumar9t2 0
  • Crash in paging when calling adapter#refresh

    Crash in paging when calling adapter#refresh

    Hi While testing your paging library I noticed this crash when refreshing page:

    java.lang.IllegalStateException: Realm accessed from incorrect thread. in /tmp/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsResults.cpp line 461
            at io.realm.internal.OsResults.nativeIsValid(Native Method)
            at io.realm.internal.OsResults.isValid(OsResults.java:700)
            at io.realm.OrderedRealmCollectionImpl.isValid(OrderedRealmCollectionImpl.java:78)
            at io.realm.RealmResults.isValid(RealmResults.java:71)
            at dev.arunkumar.compass.paging.RealmTiledDataSource._init_$lambda-1(RealmTiledDataSource.kt:111)
            at dev.arunkumar.compass.paging.RealmTiledDataSource.$r8$lambda$bRdLcJUylTJ0at1Xd1D1b6ya11s(Unknown Source:0)
            at dev.arunkumar.compass.paging.RealmTiledDataSource$$ExternalSyntheticLambda0.onInvalidated(Unknown Source:2)
            at androidx.paging.DataSource$invalidateCallbackTracker$1.invoke(DataSource.kt:103)
            at androidx.paging.DataSource$invalidateCallbackTracker$1.invoke(DataSource.kt:103)
            at androidx.paging.InvalidateCallbackTracker.invalidate$paging_common(InvalidateCallbackTracker.kt:89)
            at androidx.paging.DataSource.invalidate(DataSource.kt:395)
            at androidx.paging.LegacyPagingSource$2.invoke(LegacyPagingSource.kt:50)
            at androidx.paging.LegacyPagingSource$2.invoke(LegacyPagingSource.kt:48)
            at androidx.paging.PagingSource$invalidateCallbackTracker$1.invoke(PagingSource.kt:84)
            at androidx.paging.PagingSource$invalidateCallbackTracker$1.invoke(PagingSource.kt:84)
            at androidx.paging.InvalidateCallbackTracker.invalidate$paging_common(InvalidateCallbackTracker.kt:89)
            at androidx.paging.PagingSource.invalidate(PagingSource.kt:336)
            at androidx.paging.PageFetcher.generateNewPagingSource(PageFetcher.kt:204)
            at androidx.paging.PageFetcher.access$generateNewPagingSource(PageFetcher.kt:31)
            at androidx.paging.PageFetcher$generateNewPagingSource$1.invokeSuspend(Unknown Source:15)
            (Coroutine boundary)
    
    bug 
    opened by ggajews 2
Releases(v1.0.1)
  • v1.0.1(Nov 19, 2021)

    Changelog

    • Publish JavaDoc and Sources to maven central

    Compass

    Kotlin API and tools to make working with Realm easier

    Components

    Compass is designed to make working with Realm easier through collection of Kotlin types and extensions that handle Realm's lifecycle and threading model effectively. It has two major components

    • compass - The core Kotlin API with set of extensions for common patterns around Realm's lifecycle and threading.
    • compass-paging - Provides extensions to integrate with Jetpack Paging 3.
    • more coming soon..™

    Getting Started

    Compass is available as android library artifacts on mavenCentral. In root build.gradle:

    allprojects {
      repositories {
        mavenCentral()
      }
    }
    

    Or in dependenciesResolutionManagement in settings.gradle:

    dependencyResolutionManagement {
      repositories {
        mavenCentral()
      }
    }
    

    Then in your modules:

    
    dependencies {
        implementation "dev.arunkumar.compass:compass:1.0.1"
        // Paging integration
        implementation "dev.arunkumar.compass:compass-paging:1.0.1"
    }
    

    Resources:

    https://arunkumar.dev/introducing-compass-effective-paging-with-realm-and-jetpack-paging-3/

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Oct 9, 2021)

    Compass

    Kotlin API and tools to make working with Realm easier

    Components

    Compass is designed to make working with Realm easier through collection of Kotlin types and extensions that handle Realm's lifecycle and threading model effectively. It has two major components

    • compass - The core Kotlin API with set of extensions for common patterns around Realm's lifecycle and threading.
    • compass-paging - Provides extensions to integrate with Jetpack Paging 3.
    • more coming soon..™

    Getting Started

    Compass is available as android library artifacts on mavenCentral. In root build.gradle:

    allprojects {
      repositories {
        mavenCentral()
      }
    }
    

    Or in dependenciesResolutionManagement in settings.gradle:

    dependencyResolutionManagement {
      repositories {
        mavenCentral()
      }
    }
    

    Then in your modules:

    
    dependencies {
        implementation "dev.arunkumar.compass:compass:1.0.0"
        // Paging integration
        implementation "dev.arunkumar.compass:compass-paging:1.0.0"
    }
    

    Resources:

    https://arunkumar.dev/introducing-compass-effective-paging-with-realm-and-jetpack-paging-3/

    Source code(tar.gz)
    Source code(zip)
Owner
Arunkumar
Lead Android Engineer @ Grab
Arunkumar
A simple ToDo app to demonstrate the use of Realm Database in android to perform some basic CRUD operations like Create, Update and Delete.

Creating a Realm Model Class @RealmClass open class Note() : RealmModel { @PrimaryKey var id: String = "" @Required var title: String

Joel Kanyi 15 Dec 18, 2022
Samples demonstrating the usage of Realm-Kotlin SDK

Realm-Kotlin Samples This repository contains a set of projects to help you learn about using Realm-Kotlin SDK Each sample demonstrates different use

Realm 52 Dec 31, 2022
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

README DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to gener

Andrew Grosner 4.9k Dec 30, 2022
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

README DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to gener

Andrew Grosner 4.9k Dec 30, 2022
An Android helper class to manage database creation and version management using an application's raw asset files

THIS PROJECT IS NO LONGER MAINTAINED Android SQLiteAssetHelper An Android helper class to manage database creation and version management using an app

Jeff Gilfelt 2.2k Dec 23, 2022
Room Database Queries with Kotlin Flow

Room Database Queries with Flow This app displays a list of bus stops and arrival times. Tapping a bus stop on the first screen will display a list of

asifj96 0 Apr 26, 2022
Insanely easy way to work with Android Database.

Sugar ORM Insanely easy way to work with Android databases. Official documentation can be found here - Check some examples below. The example applicat

null 2.6k Dec 16, 2022
a 3d database ORM experiment. (used in two commercial projects)

Android-TriOrm a 3d database ORM experiment for Android. (used in two commercial projects). based around small tables concept and JVM Serialization. H

Tomer Shalev 19 Nov 24, 2021
An Android library that makes developers use SQLite database extremely easy.

LitePal for Android 中文文档 LitePal is an open source Android library that allows developers to use SQLite database extremely easy. You can finish most o

Lin Guo 7.9k Dec 31, 2022
Insanely easy way to work with Android Database.

Sugar ORM Insanely easy way to work with Android databases. Official documentation can be found here - Check some examples below. The example applicat

null 2.6k Jan 9, 2023