Skip to content

Commit

Permalink
GitBook: [develop] 2 pages modified
Browse files Browse the repository at this point in the history
  • Loading branch information
agrosner authored and gitbook-bot committed Feb 17, 2021
1 parent 2d85b79 commit 38e5845
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
25 changes: 21 additions & 4 deletions usage2/rxjavasupport.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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.
RXJava support in DBFlow is an _incubating_ feature and likely to change over time. We support RXJava3 only and have made the extensions + DBFlow compatibility almost identical - save for the changes and where it makes sense in each version.

Currently it supports

Expand All @@ -27,9 +27,11 @@ dependencies {

## Wrapper Language

Using the classes is as easy as wrapping all SQL wrapper calls with `RXSQLite.rx()` \(Kotlin we supply extension method\):
We can convert wrapper queries into different kinds of RX operations.

Before:
For a single query:

Using vanilla transactions:

```kotlin
(select
Expand All @@ -38,7 +40,7 @@ Before:
.execute { _, r ->}
```

After:
Using RX:

```kotlin
(select
Expand All @@ -50,6 +52,21 @@ After:
}
```

### Observable Queries

We can easily observe a query for any table changes \(once per transaction\) while it is active and recompute via:

```kotlin
(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:
Expand Down
54 changes: 16 additions & 38 deletions usage2/usage/databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ val db = FlowManager.getDatabase(AppDatabase::class.java)
To specify a custom **name** to the database, in previous versions of DBFlow \(< 4.1.0\), you had to specify it in the `@Database` annotation. As of 5.0 now you pass it in the initialization of the `FlowManager`:

```kotlin
FlowManager.init(context) {
database<AppDatabase> {
databaseName("AppDatabase")
}
}
FlowManager.init(FlowConfig.builder()
.database(DatabaseConfig.builder(AppDatabase::class)
.databaseName("AppDatabase")
.build())
.build())
```

To dynamically change the database name, call:
Expand All @@ -47,7 +47,7 @@ To dynamically change the database name, call:
database<AppDatabase>()
.reopen(DatabaseConfig.builder(AppDatabase::class)
.databaseName("AppDatabase-2")
.build())
.build())]
```

This will close the open DB, reopen the DB, and replace previous `DatabaseConfig` with this new one. Ensure that you persist the changes to the `DatabaseConfig` somewhere as next time app is launched and DBFlow is initialized, the new config would get overwritten.
Expand All @@ -57,11 +57,11 @@ This will close the open DB, reopen the DB, and replace previous `DatabaseConfig
As with **name**, in previous versions of DBFlow \(&lt; 5.0\), you specified `inMemory` in the `@Database` annotation. Starting with 5.0 that is replaced with:

```kotlin
FlowManager.init(context) {
inMemoryDatabase<AppDatabase> {
databaseName("AppDatabase")
}
}
FlowManager.init(FlowConfig.builder()
.database(DatabaseConfig.inMemoryBuilder(AppDatabase::class.java)
.databaseName("AppDatabase")
.build())
.build())
```

This will allow you to use in-memory databases in your tests, while writing to disk in your apps. Also if your device the app is running on is low on memory, you could also swap the DB into memory by calling `reopen(DatabaseConfig)` as explained above.
Expand Down Expand Up @@ -122,32 +122,10 @@ class CustomFlowSQliteOpenHelper(context: Contect, databaseDefinition: DatabaseD
Then in your `DatabaseConfig`:

```kotlin
FlowManager.init(context) {
database<CipherDatabase> {
openHelper(::CustomFlowSQliteOpenHelper)
}
}
```

### Database Configuration DSL

As of 5.0.0-alpha2, you can configure a database via a DSL rather than use the `FlowConfig.Builder` , `DatabaseConfig.Builder`, and `TableConfig.Builder`. This allows more readable, expressive syntax.

**Initializing DBFlow:**

```kotlin
FlowManager.init(context) {
// this is FlowConfig.Builder
database<AppDatabase> {
// this is DatabaseConfig.Builder
table<MyTable> {
// this is TableConfig.Builder
}
}
// other module dbs
databaseHolder<MyGeneratedDatabaseHolder>()
}
FlowManager.init(FlowConfig.builder(context)
.database(DatabaseConfig.Builder(CipherDatabase::class.java)
.openHelper(::CustomFlowSQliteOpenHelper)
.build())
.build())
```

By utilizing Kotlin DSL, this code is more straightforward, concise, and readable.

0 comments on commit 38e5845

Please sign in to comment.