Last active
March 8, 2018 06:41
-
-
Save umair13adil/4379fac040bddae354d6a38a08764369 to your computer and use it in GitHub Desktop.
Flowables (RxJava2) with Realm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tasksList: RealmList<Tasks> | |
tasksList = RealmList() | |
getPendingTasks(3) | |
.flatMapIterable { it -> it } | |
.concatMap { task -> | |
getPendingSubTasks(task.id) | |
.doOnNext { | |
//Here we are binding subtasks to individual task item | |
bindChildTasks(it, task) | |
} | |
}.toList() | |
.subscribeOn(looperScheduler) | |
.unsubscribeOn(looperScheduler) | |
.observeOn(looperScheduler) | |
.doOnSubscribe { | |
showLoading("") | |
}.subscribeBy( // named arguments for lambda Subscribers | |
onError = { | |
it.printStackTrace() | |
}, onSuccess = { | |
/Finalized Composed list will be used on MainThread in this callback | |
loadTasks() | |
} | |
) | |
//Child tasks will be added using Realm transaction | |
fun bindChildTasks(subTaskList: List<SubTasks>, task: Tasks) { | |
if (subTaskList.size > 0) { | |
Realm.getDefaultInstance().executeTransaction { | |
val realmList = RealmList<SubTasks>() | |
realmList.addAll(subTaskList) | |
task.subTasksList = realmList | |
} | |
} | |
} | |
//Our composed list will be fetched in this observable: | |
fun loadTasks() { | |
getPendingTasks(3) | |
.subscribeOn(AndroidSchedulers.mainThread()) | |
.unsubscribeOn(AndroidSchedulers.mainThread()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribeBy( // named arguments for lambda Subscribers | |
onNext = { | |
tasksList.addAll(it) | |
}, | |
onError = { | |
it.printStackTrace() | |
}, | |
onComplete = { | |
//We populate our RecyclerView here | |
setUpAdapter() | |
hideLoading() | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment