The aim of this project is to provide a simple example on how to observe a Realm entity and auto-update some view/s.
- Kotlin: 1.1.3-2
- RxKotlin: 2.1.0
- RxAndroid: 2.0.1
- Realm: 3.5.0
Due to a Realm dependency with RxJava 1, if you use RxJava 2 or RxKotlin 2 you can't use the default methods to convert queries to Observables provided by the Realm library.
You can see more information in this Realm project issue ticket.
This a common functionality of many Android applications. For example, if you have a functionality to mark some entity as favorite you can achieve this using this technique.
Due to the limitation above commented, we have to create an Observable manually and add the Realm entity change listener on it. For example:
class BookChangeObservable : Observable<Book>() {
lateinit var bookToObserve: Book
override fun subscribeActual(observer: Observer<in Book>?) {
object : HandlerThread("realm") {
override fun onLooperPrepared() {
super.onLooperPrepared()
val realm = Realm.getDefaultInstance()
bookToObserve = realm.where(Book::class.java).equalTo("title", MainActivity.bookTitle).findFirstAsync()
bookToObserve.addChangeListener<Book> { book, _ ->
observer!!.onNext(realm.copyFromRealm(book))
}
}
}.start()
}
}
Be sure to get a reference to the object you want to observe, to prevent the object to be garbage collected.
Now, we can subscribe to the above Observable and updates the view every time the Realm entity is updated.
Copyright 2017 Adrián Santalla
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
https://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.