0

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 Answer 1

0

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Akarnokd, I applied the changes you suggested but, when I'm inside 'subscribe' and printing 'item, it is always zero. The method 'getAllImagesFromFirebaseStorage' is returning an integer and with that the progress indicator should update but always returns zero. What could it be?
Can't tell without knowing what your method does. It just returns one value so if that value does not depend on its inputs or external factors, you get the same value every time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.