Android Rxjava订阅了变量

时间:2016-08-03 08:48:58

标签: android rx-java observer-pattern

我正在学习观察者模式,我希望我的observable能够在改变它的值时跟踪某个变量并做一些操作,我做过类似的事情:

public class Test extends MyChildActivity {

   private int VARIABLE_TO_OBSERVE = 0;

   Observable<Integer> mObservable = Observable.just(VARIABLE_TO_OBSERVE);  

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

   public void changeVariable() {
       VARIABLE_TO_OBSERVE = 1;
   }

   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*/}

}

但doMethod2()不会被调用

3 个答案:

答案 0 :(得分:32)

如果对此感兴趣的是一个Kotlin版本的Variable类,它允许订阅者在每次变量更新后进行更新。

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

用法:

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

这将打印:

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

答案 1 :(得分:31)

生活中没有什么是神奇的:如果你更新一个值,你的Observable将不会被通知。你必须自己做。例如,使用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*/}

 }

答案 2 :(得分:12)

@dwursteisen没有什么是神奇的,不,但我认为我们可以让它变得更神奇......

以这种方式使用Rx BehaviourSubject怎么样:

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);     }
}

如果希望观察代码看到初始值,请删除.skip(1)

变量支持保留在BehaviourSubject,可以通过传统的Java Getter / Setter访问。这当然是一个玩具的例子:如果你的用例真的很简单,那么不仅仅是写作的借口:

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 );
}

...但是使用BehaviourSubject可以将更改桥接到类中的其他Rx数据流,以构成更高级的行为。