我怎么知道我的 Observable class 发生了什么变化?
How do I know what changed in my Observable class?
所以我在我的应用程序中使用 Observer
模式,以便在另一个 class 中获得更改通知,而无需查找它们。
我有一个扩展 Observable
的 Singleton
class。在这个 class 里面我有 两个 CountDownTimer
类型的变量。每一个都包含两种方法:onTick()
和 onFinished()
.
为了简单起见,我们将这些定时器称为 A 和 B。
每次调用A.onTick()
、A.onFinished()
、B.onTick()
、B.onFinished()
,我必须调用notifyObservers()
通知我的Observer
有些东西变了。
到这里一切正常。问题是我知道有些事情发生了变化,但我不知道到底发生了什么变化。根据通知我的人,我必须在 Observer
端执行一些代码。
我怎么知道这些方法中的哪一个通知了我?
您可以创建自定义 EventType
class 并将其传递给 Observable.notifyObservers(Object arg)
:
public class EventType {
String eventType; //"onTick" or "onFinish"
TimerType timerType;
EventType(String eventType, TimerType timerType){
this.eventType = eventType;
this.timerType = timerType;
}
}
TimerType
是枚举类型:
public enum TimerType {
A,
B;
}
并创建 TimerA
和 TimerB
classes 扩展 CountDownTimer
:
private class TimerA extends CountDownTimer {
final EventType onTickEvent = new EventType("onTick", TimerType.A);
final EventType onFinishEvent = new EventType("onFinish", TimerType.A);
@Override
public void onTick(long millisUntilFinished) {
notifyObservers(onTickEvent);
}
@Override
public void onFinish() {
notifyObservers(onFinishEvent)
}
}
Observer
将通过 arg
参数中的 update(Observable o, Object arg);
接收 EventType
实例
使用 LiveData
而不是 Observable
。 LiveData
非常有用,因为它不仅是可观察的,而且还绑定到您的 activity 的生命周期,因此您不必担心自己处理它。
也许这个例子会对你有所帮助:
public class MyTimerWrapper {
public static MyTimerWrapper getInstance() {
// Your singleton logic
createTimers();
return instance;
}
private CountDownTimer timerA;
private CountDownTimer timerB;
private MutableLiveData<TimerEvent> timerALiveData = new MutableLiveData<TimerEvent>();
private MutableLiveData<TimerEvent> timerBLiveData = new MutableLiveData<TimerEvent>();
public LiveData<TimerEvent> startTimerA() {
timerA.start();
return timerALiveData;
}
public LiveData<TimerEvent> startTimerB() {
timerB.start();
return timerBLiveData;
}
private void createTimers() {
createTimerA();
createTimerB();
}
private void createTimerA() {
timerA = new CountDownTimer(30000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// If you're running on another thread
timerALiveData.postValue(TimerEvent.TICK);
// Otherwise
timerALiveData.setValue(TimerEvent.TICK);
}
@Override
public void onFinish() {
// If you're running on another thread
timerALiveData.postValue(TimerEvent.FINISH);
// Otherwise
timerALiveData.setValue(TimerEvent.FINISH);
}
}
}
private void createTimerB() {
// Same as createTimerA, but with timerB
}
}
public enum TimerEvent {
TICK,
FINISH
}
现在观察您 activity 中的数据:
MyTimerWrapper timerWrapper = MyTimerWrapper.getInstance();
timerWrapper.startTimerA().observe(this, new Observer {
@Override
public void onChanged(TimerEvent timerEvent) {
// Here you'll be able to see whether timerA is ticking or finished
}
})
所以我在我的应用程序中使用 Observer
模式,以便在另一个 class 中获得更改通知,而无需查找它们。
我有一个扩展 Observable
的 Singleton
class。在这个 class 里面我有 两个 CountDownTimer
类型的变量。每一个都包含两种方法:onTick()
和 onFinished()
.
为了简单起见,我们将这些定时器称为 A 和 B。
每次调用A.onTick()
、A.onFinished()
、B.onTick()
、B.onFinished()
,我必须调用notifyObservers()
通知我的Observer
有些东西变了。
到这里一切正常。问题是我知道有些事情发生了变化,但我不知道到底发生了什么变化。根据通知我的人,我必须在 Observer
端执行一些代码。
我怎么知道这些方法中的哪一个通知了我?
您可以创建自定义 EventType
class 并将其传递给 Observable.notifyObservers(Object arg)
:
public class EventType {
String eventType; //"onTick" or "onFinish"
TimerType timerType;
EventType(String eventType, TimerType timerType){
this.eventType = eventType;
this.timerType = timerType;
}
}
TimerType
是枚举类型:
public enum TimerType {
A,
B;
}
并创建 TimerA
和 TimerB
classes 扩展 CountDownTimer
:
private class TimerA extends CountDownTimer {
final EventType onTickEvent = new EventType("onTick", TimerType.A);
final EventType onFinishEvent = new EventType("onFinish", TimerType.A);
@Override
public void onTick(long millisUntilFinished) {
notifyObservers(onTickEvent);
}
@Override
public void onFinish() {
notifyObservers(onFinishEvent)
}
}
Observer
将通过 arg
参数中的 update(Observable o, Object arg);
接收 EventType
实例
使用 LiveData
而不是 Observable
。 LiveData
非常有用,因为它不仅是可观察的,而且还绑定到您的 activity 的生命周期,因此您不必担心自己处理它。
也许这个例子会对你有所帮助:
public class MyTimerWrapper {
public static MyTimerWrapper getInstance() {
// Your singleton logic
createTimers();
return instance;
}
private CountDownTimer timerA;
private CountDownTimer timerB;
private MutableLiveData<TimerEvent> timerALiveData = new MutableLiveData<TimerEvent>();
private MutableLiveData<TimerEvent> timerBLiveData = new MutableLiveData<TimerEvent>();
public LiveData<TimerEvent> startTimerA() {
timerA.start();
return timerALiveData;
}
public LiveData<TimerEvent> startTimerB() {
timerB.start();
return timerBLiveData;
}
private void createTimers() {
createTimerA();
createTimerB();
}
private void createTimerA() {
timerA = new CountDownTimer(30000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// If you're running on another thread
timerALiveData.postValue(TimerEvent.TICK);
// Otherwise
timerALiveData.setValue(TimerEvent.TICK);
}
@Override
public void onFinish() {
// If you're running on another thread
timerALiveData.postValue(TimerEvent.FINISH);
// Otherwise
timerALiveData.setValue(TimerEvent.FINISH);
}
}
}
private void createTimerB() {
// Same as createTimerA, but with timerB
}
}
public enum TimerEvent {
TICK,
FINISH
}
现在观察您 activity 中的数据:
MyTimerWrapper timerWrapper = MyTimerWrapper.getInstance();
timerWrapper.startTimerA().observe(this, new Observer {
@Override
public void onChanged(TimerEvent timerEvent) {
// Here you'll be able to see whether timerA is ticking or finished
}
})