DBFlow
Search…
RXJavaSupport
RXJava support in DBFlow is an incubating feature and likely to change over time. We support both RX1 and RX2 and have made the extensions + DBFlow compatibility almost identical - save for the changes and where it makes sense in each version.
Currently it supports
  1. 1.
    Any ModelQueriable can be wrapped in a Single, Maybe, or Flowable (to continuously observe changes).
  2. 2.
    Single + List model save(), insert(), update(), and delete().
  3. 3.
    Streaming a set of results from a query
  4. 4.
    Observing on table changes for specific ModelQueriable and providing ability to query from that set repeatedly as needed.

Getting Started

Add the separate packages to your project:
dependencies {
// RXJava3
compile "com.github.agrosner.dbflow:reactive-streams:${dbflow_version}"
}

Wrapper Language

We can convert wrapper queries into different kinds of RX operations.
For a single query:
Using vanilla transactions:
(select
from MyTable::class)
.async(db) { d -> queryList(d) }
.execute { _, r ->}
Using RX:
(select
from MyTable::class)
.async(db) { d -> queryList(d) }
.asSingle()
.subscribeBy { list ->
// use list
}

Observable Queries

We can easily observe a query for any table changes (once per transaction) while it is active and recompute via:
(select
from MyTable::class)
.asFlowable { db -> queryList(db) }
.subscribeBy { list ->
// use list
}
This works across joins as well.

Model operations

Operations are as easy as:
Person(5, "Andrew Grosner")
.rxInsert(db) // rxSave, rxUpdate, etc.
.subscribeBy { rowId ->
}

Query Stream

We can use RX to stream the result set, one at a time from the ModelQueriable using the method queryStreamResults():
(select from TestModel::class)
.queryStreamResults(db)
.subscribeBy { model ->
}
Copy link
On this page
Getting Started
Wrapper Language
Observable Queries
Model operations
Query Stream