Can't get results from RXJava subscribe

25 views Asked by At

I'm new to RXJava. I have this code

Observable.just(
    getAllImagesFromFirebaseStorage(
                                    spotsList.get(i).getName(),
                                    spotsList.get(i).getZones().get(j).getZoneImage(),
                                    outputStream
                                )
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(item -> {
    Log.i("test", "inside subscribe rxjava");
    linearProgressIndicator.setProgressCompat(item, true);
}).dispose();

I'm calling the method getAllImagesFromFirebaseStorage that downloads images from firebase and stores them in internal memory, but it freezes the UI so I'm trying to implement RXJava so that the method can be performed in a secondary thread.

getAllImagesFromFirebaseStorage returns an Integer which should be updating the linearProgressIndicator

I've been watching tutorials on implementing RXJava but it doesn't seem to work for me because it enters in the .subscribe part of the code immediately and item is always zero.

I understand that on .subscribe I'm getting the response of the method I'm calling on the secondary thread, is that so?

What can I be doing wrong?

1

There are 1 answers

2
akarnokd On

Two things are wrong.

  1. Observable.just signals a constant value that has been created before RxJava is even involved. Pretty much, getAllImagesFromFirebaseStorage runs before you even create a flow. This is a common beginner mistake.

You want to trigger computation when a subscription happens, not before that. Use fromCallable:

Observable.fromCallable(() ->
    getAllImagesFromFirebaseStorage(
                                    spotsList.get(i).getName(),
                                    spotsList.get(i).getZones().get(j).getZoneImage(),
                                    outputStream
                                )
)
  1. You call dispose on the created sequence right after you subscribe, which will immediately cancel all computation and emission. Just don't call it.