I'm struggling to understand how to properly use RxBinding
, if I want to call a network request when a user swipes down on a SwipeRefreshLayout
, I would expect to say something like
RxSwipeRefreshLayout.refreshes(swipeContainer)
.flatMap { networkRequest() }
.subscribeBy(
onNext = { list: List<Data> -> Timber.d(data) },
onError = { showErrorSnackbar(it) },
onComplete = { Timber.d("On Complete") })
But this doesn't work for me, because I have that wrapped in a function called setupSwipeRefresh()
which I call in onStart
, so as soon as onStart
is called the network request is made because that's when the layout is subscribed to.
Now I feel unsure about what to do. I could just put the whole thing in refreshListener
but that kind of defeats the purpose of RxBinding
.
Or I could execute the networkRequest
in the onNext
of the swipeContainer
. But then it would look something like
RxSwipeRefreshLayout.refreshes(swipeContainer)
.subscribeBy(
onNext = {
networkRequest()
.subscribeBy(
onNext = { list: List<Data> ->
Timber.d(data)
})
},
onError = { showErrorSnackbar(it) },
onComplete = { Timber.d("On Complete") })
But calling subscribe twice just seems like an Anti-Pattern,
So yeah, since SwipeRefreshLayout
is in the RxBinding
library, there must be an idiomatic way of doing this, because it seems like the most common use case.
You are looking for something like this:
Events
SwitchMap is like FlatMap but it will switch over to the new observable and discard incomming events from previouse observable.