Android Rxjava subscribe to a variable change

帅比萌擦擦* 提交于 2019-11-28 20:22:22

Nothing is magic in the life : if you update a value, your Observable won't be notified. You have to do it by yourself. For example using a PublishSubject.

public class Test extends MyChildActivity {

    private int VARIABLE_TO_OBSERVE = 0;

    Subject<Integer> mObservable = PublishSubject.create();  

   protected void onCreate() {/*onCreate method*/
        super();
        setContentView();
        method();
        changeVariable();
    }

    public void changeVariable() {
        VARIABLE_TO_OBSERVE = 1;
        // notify the Observable that the value just change
        mObservable.onNext(VARIABLE_TO_OBSERVE);
    }

   public void method() {
       mObservable.map(value -> {
           if (value == 1) doMethod2();
           return String.valueOf(value);
       }).subScribe(string -> System.out.println(string));
   }

   public void doMethod2() {/*Do additional operations*/}

 }

If interested here a Kotlin version of Variable class, which lets subscribers to be updated after every variable change.

class Variable<T>(private val defaultValue: T) {
var value: T = defaultValue
    set(value) {
        field = value
        observable.onNext(value)
    }
val observable = BehaviorSubject.createDefault(value)
}

Usage:

val greeting = Variable("Hello!")
greeting.observable.subscribe { Log.i("RxKotlin", it) }
greeting.value = "Ciao!"
greeting.value = "Hola!"

This will print:

"Hello!"
"Ciao!"
"Hola!"

@dwursteisen Nothing is magic, no, but I think we can get it a little more magic than that... 😊

How about using an Rx BehaviourSubject in this way:

import rx.functions.Action1;
import rx.subjects.BehaviorSubject;    

public class BehaviourSubjectExample {

    public BehaviourSubjectExample() {
        subject.skip(1).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                System.out.println("The value changed to " + integer );
            }
        });
    }

    public final BehaviorSubject<Integer> subject = BehaviorSubject.create(0);

    public int  getValue()          { return subject.getValue(); }
    public void setValue(int value) { subject.onNext(value);     }
}

Remove the .skip(1) if you want the observing code to see the initial value.

The variable backing remains with the BehaviourSubject and can be accessed through conventional Java Getter/Setter. This is a toy example of course: If your use case were really this simple there'd be no excuse for not just writing:

private int value = 0;

public int  getValue() { return value; }
public void setValue(int value) {
    this.value = value;
    System.out.println("The value changed to " + value );
}

...but the use of BehaviourSubject lets you bridge changes to other Rx data-streams inside your class for composing more advanced behaviours.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!