How do I know what changed in my Observable class?

João Martins :

So I'm using the Observer pattern in my app in order to get notified of changes in another class without having to look for them.

I have a Singleton class which extends Observable. Inside this class I have two CountDownTimer type variables. Eachs of these contains two methods: onTick() and onFinished().

Let's call those Timers A and B for the sake of simplicity.

Every time A.onTick(), A.onFinished(), B.onTick(), B.onFinished() are called, I must call notifyObservers() to notify my Observer that something has changed.

Until here everything works fine. The problem is that I know something has changed, but I don't know what exactly has changed. Depending on which one notified me, I must execute some code on the Observer side.

How do I know which of these methods notified me?

92AlanC :

Use LiveData instead of Observable. LiveData is quite useful because not only it's observable but also it binds to your activity's lifecycle so you don't have to worry about handling it yourself.

Maybe this example will help you:

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
}

Now to observe that data in your 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
    }
})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174179&siteId=1