ursus
05/26/2019, 3:51 AMfun refresh(page: Int) : Completable {
.. call api
database.write(...)
}
(edited)
and now I want to display progressbar while this is happening + progressbar at the end if page > 1 (edited)
the trouble with this is that presentation layer has a query observable opened liek this
ViewModel {
database.fooQuery()
.subscribe { ... post to adapter }
}
which means that this will emit at the time database.write is called
so If map refresh to events of Started, Success, Error
Success will come after the database is refreshed
which to me is weird, since it should be done in one go, but that means refresh would need to return that data too (edited)
tldr; writing to the database is side-effecting - how to deal with that?Matej Drobnič
05/26/2019, 8:11 AMsealed class Resource<T> {
object Loading<T>() : Resource<T>
data class Success<T>(val data : T) : Resource<T>
}
fooQuery
would return Resource<Data>
refresh
you update fooQuery
to switch to Loading
and when data actually refreshes, it switches back to Success
fooQuery
and refresh
- after you call database.write
, refresh would not complete immediatelly, but would wait for fooQuery
to trigger before completingursus
05/26/2019, 1:28 PMMatej Drobnič
05/27/2019, 5:37 AMprivate var loading: Boolean = true
private val dataSubject = BehaviorProcessor.create<Resource<List<T>>>()
your fooQuery()
is mapped to the dataSubject
. Whenever new item arrives from DB, it checks for the value of loading
and wraps it around with appropriate resource.
I'm not aware of any way to actually check whether db changes were the ones you wanted or not or something else changing. But it is very likely that first database change after your write is gonna be update from this write, so I don't think it is unreasonable to just assume some things and set Success there.ursus
05/27/2019, 5:41 AM