Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving an RxJava Observable something to emit from another method

I have a variable in a Fragment that will have its value change multiple times throughout the Fragment's life. It's triggered by UI interactions, so I thought it might be a good idea to use an Observable to hold it, rather than make all of the to-be-updated views as fields and do my UI changes from a setter.

The value has to be updated through another method (basically a setter that should call onNext() on the subscriber), not through the Observable itself. Is there a way to do that with RxJava's design?

In other words, I'm looking to have an Observable field, and give it new values to emit (calling onNext() on its subscribers) from another method in the class.

like image 903
Steven Schoen Avatar asked Apr 25 '15 12:04

Steven Schoen


1 Answers

RxJava has Subjects for this purpose. For example:

private final PublishSubject<String> subject = PublishSubject.create();

public Observable<String> getUiElementAsObservable() {
    return subject;
}

public void updateUiElementValue(final String value) {
    subject.onNext(value);
}
like image 113
Vladimir Mironov Avatar answered Nov 20 '22 22:11

Vladimir Mironov