【问题标题】:How do I know what changed in my Observable class?我怎么知道我的 Observable 课程发生了什么变化?
【发布时间】:2020-06-18 07:16:08
【问题描述】:

所以我在我的应用程序中使用了Observer 模式,以便在另一个类中获得更改通知,而无需查找它们。

我有一个扩展ObservableSingleton 类。在这个类中,我有 两个 CountDownTimer 类型变量。每个都包含两个方法:onTick()onFinished()

为了简单起见,我们将这些计时器称为 AB

每次调用A.onTick()A.onFinished()B.onTick()B.onFinished() 时,我必须调用notifyObservers() 以通知我的Observer 发生了一些变化。

直到这里一切正常。问题是我知道有些东西发生了变化,但我不知道到底发生了什么变化。根据通知我的是哪一个,我必须在Observer 端执行一些代码。

我怎么知道这些方法中的哪一个通知了我?

【问题讨论】:

  • Observer/Observable 有其局限性。我建议您创建自己的实现
  • 请向我们展示课程...所有三个课程(Observerss 和 Observable,可能会将它们缩短到重要部分)。

标签: java android observable observer-pattern observers


【解决方案1】:

使用LiveData 代替ObservableLiveData 非常有用,因为它不仅是可观察的,而且还绑定到您的活动的生命周期,因此您不必担心自己处理它。

也许这个例子会对你有所帮助:

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
}

现在观察您活动中的数据:

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

【讨论】:

  • 你将如何注销观察者?我知道它适用于生命周期,但在我的情况下,生命周期不会杀死观察者。所以我需要明确地杀死它。我可以打电话给removeObservers();,但我不知道这是否也会将观察者从计时器 B 中移除。
  • 这很奇怪,因为当您观察LiveData 时,您提供了生命周期所有者(活动或片段) - 请参阅observe(this, ...),其中this 代表生命周期所有者。当您这样做时,您的活动/片段将在前台观察到您的LiveData,但是当您的生命周期所有者被销毁时,将不再观察到LiveData
  • 没错。但就我而言,我还没有破坏我的活动/片段。所以没有触发生命周期事件。无论如何,我发现removeObservers(); 不会影响不同的观察者(我希望)——我还没有完全测试。
【解决方案2】:

您可以创建一个自定义的EventType 类并将其传递给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;        
}

并创建扩展 CountDownTimerTimerATimerB 类:

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 将通过 update(Observable o, Object arg); 参数中的 update(Observable o, Object arg); 接收 EventType 实例

【讨论】:

  • 您将如何在 Observer 中的 update() 方法中访问这些类型?例如使用 switch()。
  • TimerType 通过 update() 方法传递给 Observers。然后您可以使用开关或 if (arg == TimerType.A){...}
  • 您仍然无法判断我们是通过 TICK 还是 FINISHED 通知您的
  • 然后创建一个EventType类,它有2个属性:事件类型和定时器类型。我已经更新了答案
猜你喜欢
  • 1970-01-01
  • 2011-03-19
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 1970-01-01
相关资源
最近更新 更多