public final class Monarchy
extends java.lang.Object
Not only does it auto-manage the Realm lifecycle, but specifically prevents the ability to use Realm in such a way that it is impossible to leave a Realm instance open by accident.
In case of `copied`, `mapped`, or `paged` results, the queries are evaluated on a background looper thread.
In case of `managed` results, the RealmResults is provided along with its change set.
| Modifier and Type | Class and Description |
|---|---|
static class |
Monarchy.Builder
Builder class used to build a Monarchy instance.
|
static class |
Monarchy.ManagedChangeSet<T extends RealmModel>
A class that contains the RealmResults and the OrderedCollectionChangeSet.
|
static interface |
Monarchy.Mapper<R,T>
A mapper interface that can be used in
findAllMappedWithChanges(Query, Mapper) to map out instances on the background looper thread. |
static interface |
Monarchy.Query<T extends RealmModel>
An interface used to define Realm queries, therefore bypassing the thread-local aspect of RealmQuery.
|
static interface |
Monarchy.RealmBlock
Interface to define what to do with the Realm instance.
|
| Modifier and Type | Method and Description |
|---|---|
void |
closeManually()
If the Monarchy thread was opened manually, then this method can be used to decrement the forced reference count increment.
|
void |
doWithRealm(Monarchy.RealmBlock realmBlock)
Allows to manually use Realm instance and will automatically close it when done.
|
<T extends RealmModel> |
fetchAllCopiedSync(Monarchy.Query<T> query)
Provides ability to synchronously fetch a copied RealmResults.
|
<T extends RealmModel> |
fetchAllManagedSync(Realm realm,
Monarchy.Query<T> query)
Provides ability to synchronously obtain a managed RealmResults as a List, in a safe way.
|
<T extends RealmModel,U> |
fetchAllMappedSync(Monarchy.Query<T> query,
Monarchy.Mapper<U,T> mapper)
Provides ability to synchronously fetch a mapped RealmResults.
|
<T extends RealmModel> |
findAllCopiedWithChanges(int maxDepth,
Monarchy.Query<T> query)
Returns a LiveData that evaluates the new results on a background looper thread.
|
<T extends RealmModel> |
findAllCopiedWithChanges(Monarchy.Query<T> query)
Returns a LiveData that evaluates the new results on a background looper thread.
|
<T extends RealmModel> |
findAllFrozenWithChanges(Monarchy.Query<T> query)
Returns a LiveData that evaluates the new results on a background looper thread.
|
<T extends RealmModel> |
findAllManagedWithChanges(Monarchy.Query<T> query)
Returns a LiveData that evaluates the new results on the UI thread, using Realm's Async Query API.
|
<T extends RealmModel> |
findAllManagedWithChangesSync(Monarchy.Query<T> query)
Returns a LiveData that evaluates the new results on the UI thread, using Realm's Sync Query API.
|
<T extends RealmModel,U> |
findAllMappedWithChanges(Monarchy.Query<T> query,
Monarchy.Mapper<U,T> mapper)
Returns a LiveData that evaluates the new results on a background looper thread.
|
static RealmConfiguration |
getDefaultConfiguration()
Returns the default configuration.
|
RealmConfiguration |
getRealmConfiguration() |
static void |
init(android.content.Context context)
Initializes Realm as usual, except sets a default configuration to detect if a custom default is properly set.
|
boolean |
isMonarchyThreadOpen()
Returns if the Monarchy thread is open.
|
void |
openManually()
Forcefully opens the Monarchy thread, keeping it alive until
closeManually() is called. |
void |
postToMonarchyThread(Monarchy.RealmBlock realmBlock)
Posts the RealmBlock to the Monarchy thread, and executes it there.
|
void |
runTransactionSync(Realm.Transaction transaction) |
static void |
setDefaultConfiguration(RealmConfiguration realmConfiguration)
Calls Realm.setDefaultConfiguration(config).
|
void |
writeAsync(Realm.Transaction transaction)
Writes asynchronously on a single-threaded execution pool.
|
public static void init(android.content.Context context)
context - app contextpublic static void setDefaultConfiguration(RealmConfiguration realmConfiguration)
realmConfiguration - realm configurationpublic static RealmConfiguration getDefaultConfiguration()
java.lang.IllegalStateException - if the invalid default configuration is still set.public final RealmConfiguration getRealmConfiguration()
public final void runTransactionSync(Realm.Transaction transaction)
public final void doWithRealm(Monarchy.RealmBlock realmBlock)
realmBlock - the Realm execution block in which Realm should remain openpublic final void writeAsync(Realm.Transaction transaction)
transaction - the Realm transactionpublic <T extends RealmModel> java.util.List<T> fetchAllManagedSync(Realm realm,
Monarchy.Query<T> query)
What is actually returned is a snapshot collection.
This method only makes sense either if Realm is opened manually, or inside a doWithRealm(RealmBlock) (or runTransactionSync(Realm.Transaction) method).
T - RealmObject typerealm - Realmquery - Querypublic <T extends RealmModel> java.util.List<T> fetchAllCopiedSync(Monarchy.Query<T> query)
T - RealmObject typequery - Querypublic <T extends RealmModel,U> java.util.List<U> fetchAllMappedSync(Monarchy.Query<T> query, Monarchy.Mapper<U,T> mapper)
T - RealmObject typeU - the mapped typequery - Querypublic <T extends RealmModel> <any> findAllCopiedWithChanges(Monarchy.Query<T> query)
The items are copied out with `realm.copyFromRealm(results)`.
T - the RealmModel typequery - the querypublic <T extends RealmModel> <any> findAllCopiedWithChanges(int maxDepth,
Monarchy.Query<T> query)
The items are copied out with `realm.copyFromRealm(results, maxDepth)`.
T - the RealmModel typemaxDepth - the max depthquery - the querypublic <T extends RealmModel> <any> findAllFrozenWithChanges(Monarchy.Query<T> query)
The items are frozen with `realmResults.freeze()`.
T - the RealmModel typequery - the querypublic <T extends RealmModel,U> <any> findAllMappedWithChanges(Monarchy.Query<T> query, Monarchy.Mapper<U,T> mapper)
The items are mapped out with the provided Monarchy.Mapper.
T - the RealmModel typeU - the mapped typequery - the querypublic <T extends RealmModel> <any> findAllManagedWithChanges(Monarchy.Query<T> query)
The managed change set contains the OrderedCollectionChangeSet evaluated by Realm.
T - the RealmModel typequery - the querypublic <T extends RealmModel> <any> findAllManagedWithChangesSync(Monarchy.Query<T> query)
The managed change set contains the OrderedCollectionChangeSet evaluated by Realm.
T - the RealmModel typequery - the querypublic void openManually()
closeManually() is called.public void closeManually()
This means that the Monarchy thread does not stop unless all observed LiveData are also inactive.
public boolean isMonarchyThreadOpen()
public void postToMonarchyThread(Monarchy.RealmBlock realmBlock)
realmBlock - the Realm blockjava.lang.IllegalStateException - if the Monarchy thread is not open