Sira Lam
Sira Lam

Reputation: 5367

Should I use BehaviorSubject to create an Observable from a variable, if I only want the variable to emit itself when it changes?

(First of all, although I think this is a question related to general Rx programming, but since I am developing Android and using RxJava, the below question will be asked in Android and RxJava context.)

I am new to RxJava and therefore want to implement it in some easy way first.

Scenario

I have a form, I want to enable the button only if all necessary fields in the form has been filled in.
I have already managed to implement this feature, for all EditText inputs.

Now, my form also has an image selection.
I therefore has a Bitmap variable which stores the image that user selected.
Since this image is also necessary, I need to enable the button only if this Bitmap variable is not null.

Problem

So, what I am thinking is, create an Observable from this Bitmap variable, that will emits itself (or at least a Boolean) when its value has been updated.

And then very quickly, I find some StackOverflow posts which suggest using BehaviorSubject.

The problem is, in every starter tutorial of Rx I have read, Subject has not been mentioned. And even on the official website of Rx, it has a link to an article where it said

"[Subjects] are the "mutable variables" of the Rx world and in most cases you do not need them."

So, I started to doubt whether I really should use Subject to do what I want. I suspect may be I should create an Observable from the function which updates the variable.

But then I am stuck. How can I do this?

Upvotes: 1

Views: 910

Answers (1)

akarnokd
akarnokd

Reputation: 70007

This case is that rare case where BehaviorSubject is appropriate. You can onNext the image on it which will notify observers and you can getValue() to access this image directly afterwards.

Upvotes: 2

Related Questions