[英]Android Rxjava subscribe to a variable change
I am learning Observer pattern, I want my observable to keep track of a certain variable when it changes it's value and do some operations, I've done something like : 我正在学习观察者模式,我希望我的观察者在改变它的值并做一些操作时跟踪某个变量,我做了类似的事情:
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*/}
}
But doMethod2() doesn't get called 但doMethod2()不会被调用
If interested here a Kotlin version of Variable class, which lets subscribers to be updated after every variable change. 如果对此感兴趣的是一个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)
}
Usage: 用法:
val greeting = Variable("Hello!")
greeting.observable.subscribe { Log.i("RxKotlin", it) }
greeting.value = "Ciao!"
greeting.value = "Hola!"
This will print: 这将打印:
"Hello!"
"Ciao!"
"Hola!"
Nothing is magic in the life : if you update a value, your Observable
won't be notified. 生活中没有什么是神奇的:如果你更新一个值,你的
Observable
将不会被通知。 You have to do it by yourself. 你必须自己做。 For example using a
PublishSubject
. 例如,使用
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*/}
}
@dwursteisen Nothing is magic, no, but I think we can get it a little more magic than that... 😊 @dwursteisen没有什么是神奇的,不,但我认为我们可以让它变得更神奇......😊
How about using an Rx BehaviourSubject
in this way: 如何以这种方式使用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); }
}
Remove the .skip(1)
if you want the observing code to see the initial value. 如果希望观察代码看到初始值,请删除
.skip(1)
。
The variable backing remains with the BehaviourSubject
and can be accessed through conventional Java Getter/Setter. 变量支持保留在
BehaviourSubject
,可以通过传统的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. ...但是使用
BehaviourSubject
可以将更改桥接到类中的其他Rx数据流,以构成更高级的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.