RXJava Support
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
Insert
,Update
,Delete
,Set
,Join
, and all wrapper query mechanisms by wrapping them inrx()
- Single +
List
modelsave()
,insert()
,update()
, anddelete()
. - Streaming a set of results from a query
- Observing on table changes for specific
ModelQueriable
and providing ability to query from that set repeatedly as needed. - Kotlin extension methods in a separate artifact that enhance the conversion.
Getting Started
Add the separate packages to your project:
dependencies {
// RXJava1
compile "com.github.Raizlabs.DBFlow:dbflow-rx:${dbflow_version}"
// optional, for use with Kotlin as a nice companion.
compile "com.github.Raizlabs.DBFlow:dbflow-rx-kotlinextensions:${dbflow_version}"
// RXJava2
compile "com.github.Raizlabs.DBFlow:dbflow-rx2:${dbflow_version}"
// optional, for use with Kotlin as a nice companion.
compile "com.github.Raizlabs.DBFlow:dbflow-rx2-kotlinextensions:${dbflow_version}"
}
Wrapper Language
Using the classes is as easy as wrapping all SQL wrapper calls with RXSQLite.rx()
(Kotlin we supply extension method):
Before:
List<MyTable> list = SQLite.select()
.from(MyTable.class)
.queryList();
After:
RXSQLite.rx(
SQLite.select().from(MyTable.class))
.queryList()
.subscribe((list) -> {
});
or with Kotlin + extension methods:
select.from(MyTable::class.java)
.rx()
.list { list ->
}
Model operations
To make the transition as smoothest as possible, we've provided a BaseRXModel
which replaces BaseModel
for convenience in the RX space.
class Person(@PrimaryKey var id: Int = 0, @Column var name: String? = "") : BaseRXModel
Operations are as easy as:
new Person(5, "Andrew Grosner")
.insert()
.subscribe((rowId) -> {
});
or with Kotlin+extensions:
Person(5, "Andrew Grosner")
.insert { rowId ->
}
Query Stream
We can use RX to stream the result set, one at a time from the ModelQueriable
using
the method queryStreamResults()
:
RXSQLite.rx(
SQLite.select()
.from(TestModel1.class))
.queryStreamResults()
.subscribe((model) -> {
});
Kotlin Support
Most of the support mirrors kotlin support with a few minor changes.
Extension properties/methods include:
rx()
extension method making it super easy to integrate RX.RXModelQueriable.streamResults
- stream results one at time to aSubscription
list
,result
,streamResults
,cursorResult
,statement
,hasData
,cursor
, andcount
all provide a method lambda that is called within aSubscription
.
select from MyTable::class
where (MyTable.name `is` "Good")
list { list -> //
}
which is the same with RX as:
(select.from(MyTable::class.java)
.where(MyTable.name `is` "Good"))
.rx()
.list { list ->
}
Or if we want to get pretty with BaseRXModel
+ extensions:
Person("Somebody").save { success ->
// do something
}
Person("Somebody").update { success ->
// do something
}
Person("Somebody").insert { rowId ->
// do something
}
Person("Somebody").delete { success ->
// do something
}