This is an API sample to showcase how to implement observable queries in Room, with RxJava's Flowable objects.
The sample app shows an editable user name, stored in the database.
The database is created using Room and has one entity: a User
. Room generates the corresponding SQLite table at
runtime.
Queries are executed in the UserDao
class. The user retrieval is done via an observable query implemented using a
Flowable
. Every time the user data is updated, the Flowable object will emit automatically, allowing to update the UI
based on the latest data. The Flowable will emit only when the query result contains at least a row. When there is no
data to match the query, the Flowable will not emit.
The app has a main Activity that displays the data. The Activity works with a ViewModel to do the following:
Room guarantees that the observable query will be triggered on a background thread. In the Activity, the Flowable events are set to be received on the main thread, so the UI can be updated. The insert query is synchronous so it's wrapped in a Completable and executed on a background thread. On completion, the Activity is notified on the main thread.
Copyright (C) 2017 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
compileSdkVersion build_versions.target_sdk
applicationId "com.example.android.persistence"
minSdkVersion build_versions.min_sdk
targetSdkVersion build_versions.target_sdk
versionCode 1
versionName "1.0"
compile deps.support.app_compat
compile deps.support.v4
compile deps.support.design
compile deps.lifecycle.runtime
compile deps.lifecycle.extensions
compile deps.room.runtime
compile deps.room.rxjava2
compile deps.rx_android
compile deps.rxjava2
testCompile deps.junit
testCompile deps.hamcrest
testCompile deps.arch_core.testing
testCompile deps.mockito.all
testCompile deps.support.annotations
testCompile deps.support.core_utils
package com.example.android.persistence
Main screen of the app. Displays a user name and gives the option to update the user name.