0
votes

How would I handle this in RxJava (I am just having a fairly hard time learning this library).

I have I URL that I want to download at the start of my Android app and I want it to start even if no one is subscribing to it. The download may take a certain amount of time so it is possible that the user could end up on the page that relies on the download of that url, so I want the Activity/Fragment to subscribe to that outstanding Observable.

I plan on tracking the outstanding Observables with a simple

Map<String, Observable>

so Observers (Activities/Fragments) can know if its url/string isn't in the map then their stuff is already done. I was thinking of using PublishSubject, but according to a talk I heard they suggested to not use Subject's because you should learn to do it with an Observable first before you do the "shortcuts".

1

1 Answers

0
votes

I would approach this with a PublishSubject.

PublishSubject<DownloadResult> download = PublishSubject.create();

When your app starts, you start the download, using a classic RxJava routine with .subscribeOn(Schedulers.io). When the download ends, you push the result to the PublishSubject:

download.onNext(downloadResult);
download.onCompleted();

Your Activities/Fragments Observers will subscribe to the PublishSubject and get notified when the download is completed. Piece of cake ;-)