MCPcopy Index your code
hub / github.com/Zhuinden/realm-monarchy

github.com/Zhuinden/realm-monarchy @2.2.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release 2.2.1 ↗ · + Follow
232 symbols 525 edges 38 files 56 documented · 24% updated 2mo ago2.2.1 · 2026-04-29★ 892 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Monarchy

A wrapper over Realm, that exposes RealmResults as various forms of LiveData.

With that, you can use a singleton Monarchy instance to manage Realm queries, and possibly make it easier to hide Realm as implementation of the data layer.

Adding to project

To use Monarchy, you need to add as a dependency:

implementation 'com.github.Zhuinden:realm-monarchy:2.2.1'

And it's available on Jitpack, so you need to add

allprojects {
    repositories {
        // ...
        maven { url "https://jitpack.io" }
    }
    // ...
}

In newer projects, you need to also update the settings.gradle file's dependencyResolutionManagement block:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }  // <--
        jcenter() // Warning: this repository is going to shut down soon
    }
}

How can I use it?

Initialization

Initialize Monarchy, and create a new Monarchy instance (for a given RealmConfiguration):

Monarchy.init(this); // need to call this only once
monarchy = new Monarchy.Builder()
    .setRealmConfiguration(new RealmConfiguration.Builder()
         .deleteRealmIfMigrationNeeded()
         .initialData(realm -> {
              RealmDog dog = realm.createObject(RealmDog.class);
              dog.setName("Corgi");
         }).build()
    ).build();

Queries

Create queries as LiveData, and observe them

LiveData<List<RealmDog>> dogs = monarchy.findAllCopiedWithChanges(realm -> realm.where(RealmDog.class));
dogs.observe(this, dogs -> {...});

You can also create a Mapper which will map the RealmObject to something else

LiveData<List<Dog>> dogs = monarchy.findAllMappedWithChanges(realm -> realm.where(RealmDog.class), dog -> Dog.create(dog.getName()));
dogs.observe(this, dogs -> {...});

Or even as frozen results

You can also create a Mapper which will map the RealmObject to something else

LiveData<List<RealmDog>> dogs = monarchy.findAllFrozenWithChanges(realm -> realm.where(RealmDog.class));
dogs.observe(this, dogs -> {...});

You can also synchronously get mapped/copied results, but it's generally not recommended; it is preferred to find results with changes instead.

Writes

You can do either synchronous transaction, or have the transaction be executed asynchronously on a dedicated single-threaded pool.

For synchronous transaction, you can use runTransactionSync(Realm.Transaction).

monarchy.runTransactionSync(realm -> {
    RealmDog dog = realm.createObject(RealmDog.class);
    dog.setName("Doge");
});

For asynchronous transaction, you can use writeAsync(Realm.Transaction).

monarchy.writeAsync(realm -> {
    RealmDog dog = realm.createObject(RealmDog.class);
    dog.setName("Doge");
});

Working with Realm instances

Instead of using Realm.getDefaultInstance() and close(), now you should just do

monarchy.doWithRealm((realm) -> {
    ....
});

And otherwise expose the queries as LiveData, and observe them. Whether a LiveData has observers or not will properly manage the Realm lifecycle.

Information

Listening for copied/mapped results happens on the background looper thread.

Listening for paged results happens on the background looper thread.

Listening for frozen results happens on the background looper thread.

Listening for managed results happens on the UI thread.

Possible FAQs

Why isn't this written with Rx operators?

Because managing ref counting and doing specific callbacks when ref-counting is tricky, while LiveData makes it trivial.

Observable.just(5)
          .replay(1)
          .autoConnect(0)
          .doOnSubscribe(/* onActive */)
          .doOnUnsubscribe(/* onInactive */)
          .subscribe()

or something like that. Tricky stuff. So if you need LiveData exposed to Rx, then just use:

Flowable<List<Dog>> dogs = Flowable.fromPublisher(LiveDataReactiveStreams.toPublisher(lifecycleOwner, liveData));

How do I open a Realm instance and close it manually, without being in a block?

This is where most errors in Realm usage come from, so it is just generally not recommended.

Realm already manages a reference counted cache for thread-local Realm instances, where of course getInstance() increases ref count. So if that doesn't suit you, feel free to keep your own ThreadLocal<Realm> cache.

When should I compact the Realm?

Probably when you've finished every Activity. When's that? If you have only 1 finishing Activity, then it's easy, if you have more Activities, then that's a different problem :D

Why is this library possible?

Because LiveData made it possible.

License

Copyright 2017 Gabor Varadi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Extension points exported contracts — how you extend this code

Query (Interface)
An interface used to define Realm queries, therefore bypassing the thread-local aspect of RealmQuery. @param the re [3 …
monarchy/src/main/java/com/zhuinden/monarchy/Monarchy.java
CustomDiffResult (Interface)
(no doc) [2 implementers]
monarchy-example/src/main/java/com/zhuinden/monarchyexample/utils/CustomDiffResult.java
RealmBlock (Interface)
Interface to define what to do with the Realm instance. [3 implementers]
monarchy/src/main/java/com/zhuinden/monarchy/Monarchy.java
ApplicationComponent (Interface)
Created by Zhuinden on 2017.12.21..
monarchy-example/src/main/java/com/zhuinden/monarchyexample/application/injection/ApplicationComponent.java
Mapper (Interface)
A mapper interface that can be used in Monarchy#findAllMappedWithChanges(Query, Mapper) to map out instances on
monarchy/src/main/java/com/zhuinden/monarchy/Monarchy.java

Core symbols most depended-on inside this repo

get
called by 27
monarchy-example/src/main/java/com/zhuinden/monarchyexample/application/MainActivity.java
setName
called by 11
monarchy-example/src/main/java/com/zhuinden/monarchyexample/RealmDog.java
writeAsync
called by 10
monarchy/src/main/java/com/zhuinden/monarchy/Monarchy.java
createQuery
called by 8
monarchy/src/main/java/com/zhuinden/monarchy/Monarchy.java
name
called by 8
monarchy-example/src/main/java/com/zhuinden/monarchyexample/Dog.java
stopListening
called by 7
monarchy/src/main/java/com/zhuinden/monarchy/Monarchy.java
getInjector
called by 7
monarchy-example/src/main/java/com/zhuinden/monarchyexample/application/CustomApplication.java
inject
called by 7
monarchy-example/src/main/java/com/zhuinden/monarchyexample/application/injection/ApplicationComponent.java

Shape

Method 184
Class 42
Interface 6

Languages

Java100%

Modules by API surface

monarchy/src/main/java/com/zhuinden/monarchy/Monarchy.java45 symbols
monarchy/src/main/java/com/zhuinden/monarchy/ManagedLiveResults.java14 symbols
monarchy-example/src/main/java/com/zhuinden/monarchyexample/features/mapped_rx/MappedRxFragment.java11 symbols
monarchy-example/src/main/java/com/zhuinden/monarchyexample/features/mapped_rx/MappedRxDogAdapter.java9 symbols
monarchy-example/src/main/java/com/zhuinden/monarchyexample/features/mapped/MappedDogAdapter.java9 symbols
monarchy-example/src/main/java/com/zhuinden/monarchyexample/features/managed/ManagedDogAdapter.java9 symbols
monarchy-example/src/main/java/com/zhuinden/monarchyexample/features/frozen/FrozenDogAdapter.java9 symbols
monarchy-example/src/main/java/com/zhuinden/monarchyexample/features/copied/CopiedDogAdapter.java9 symbols
monarchy-example/src/main/java/com/zhuinden/monarchyexample/application/MainActivity.java9 symbols
monarchy/src/main/java/com/zhuinden/monarchy/MappedLiveResults.java8 symbols
monarchy/src/main/java/com/zhuinden/monarchy/CopiedLiveResults.java8 symbols
monarchy/src/main/java/com/zhuinden/monarchy/FrozenLiveResults.java7 symbols

For agents

$ claude mcp add realm-monarchy \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page