Hi folks, how would I prevent showing a loading in...
# rx
i
Hi folks, how would I prevent showing a loading indicator when the data is "already there"? Let me explain: • We perform some api requests, typically `Single`s and show a loading indicator in
doOnSubscribe
• Now we navigate forwards and back. This causes the observer (fragment/view) to re-subscribe. • The api calls are not performed again, but
doOnSubscribe
is called, making the progress indicator show for a very short moment. -> How can we show the indicator only when the requests are being performed?
p
I solved yesterday similar problem - before calling async process operation im checking synchronously if file (data) exists in storage. If yes, open sync, else run async file generation (including fetching data from db). Other solution is creating observable which emits state of process. Like PROCESSING, COMPLETE, START, etc. Then if data isn't available, startwith(START) - on UI start process bar. Else - return complete state. For detail explanation, I recommend Jake Warthon's managing state video
i
Okay, the former isn't usable for us, since we're not storing the data. It's "cached in memory" (i.e. the result of the
Single
) vs. "nothing"
for the second I'd not be sure how to implement this with retrofit? The calls are retrofit `Single`s
I guess this could be implemented in some sort of middleware, but sounds a bit long task for what I was planing now 😕
alternatively there's the option to make the indicator time-dependent, i.e. show only if when the call takes long like described here https://stackoverflow.com/questions/34326683/how-do-you-show-spinner-if-rxjava-observable-takes-to-long 🤔
z
So if the data is already available, it emits immediately? By default, that should happen synchronously, within the same frame, so the loading indicator shouldn't have the chance to display. Sounds like something is using a scheduler somewhere. Could be in retrofit, in your cache layer, maybe LiveData, or anywhere in between. How is your cache implemented? Do you use
subscribeOn
or
observeOn
anywhere?
i
@Zach Klippenstein (he/him) [MOD] just wanted to say shortly that I've not forgotten this. Been busy, will come back to this as soon as I can.
z
It’s your question, no rush on my end 🙂
i
@Zach Klippenstein (he/him) [MOD] there's no cache. Only an observable in the view model. I'm using
subscribeOn
/
observeOn
to call the remote request using the IO scheduler
I'm considering solving this by adding a
BehaviorSubject
to the view model (as cache) and showing the progress indicator only if it's not set yet
this is what I ended with: • Added
BehaviorSubject
to view model with the downloaded data, subscribe to this in fragment • Added an
onFragmentCreated()
function to view model, where I fetch the data from the api and set the behavior subject The fragment's
onCreate
is called only when navigating (forwards) to it, so it behaves as expected.