rx-realm
mulab
58
Visit GitHub Repo
NoSQL

** Deprecated! From realm-java 0.87.0 and on, it has builtin observable support **

Android Arsenal Build Status

rx-realm

A lightweight wrapper around realm-java which introduces reactive stream semantics to SQL operations.(Inspired by square/sqlbrite)

Setup

Add the JitPack repository to your build file:

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

Add the dependency:

dependencies {
    compile 'com.github.mulab:rx-realm:1.2.0'
    compile 'io.realm:realm-android:0.86.1'
}

Initialize (in Appliction#onCreate method for Android App):

RealmConfiguration config = new RealmConfiguration.Builder(context)
    // set configuration for realm, see realm-java's document
    .build();
RealmDatabase.init(config);

Usages

Assume that Foo is a realm data model, i.e. Foo extends RealmObject.(see more in Realm-java's documentation)

Query

RealmDatabase.createQuery(new Query()<Foo>{
    @Override
    public RealmResults<Foo> call(Realm realm) {
        return realm.where(Foo.class).findAll();
    }
}, Foo.class).subscribe(this);

The last parameter of createQuery means watching on Foo if it is notified.

Exec

RealmDatabase.exec(new Exec() {
    @Override
    public void run(Realm realm) {
        realm.where(Foo.class).findAll().clear();
    }
}, Foo.class).subscribe(this);

The last parameter of exec means it will notify Foo.

Become Pro in Android by watching videos

OUR LEARNERS WORK AT