Multiple calls to set LiveData is not observed
Asked Answered
C

5

17

I have recently seen a weird issue that is acting as a barrier to my project. Multiple calls to set the live data value does not invoke the observer in the view.

It seems that only the last value that was set actually invokes the Observer in the view.

Here is the code snippet for a review.

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private lateinit var viewModel: MainViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        viewModel = ViewModelProviders.of(this).get(MainViewModelImpl::class.java)

        viewModel.state().observe(this, Observer {
            onStateChange(it!!)
        })

        viewModel.fetchFirstThree()

    }

    private fun onStateChange(state: MainViewModel.State) {

        when (state) {
            is One -> {
                show(state.data)
            }
            is Two -> {
                show(state.data)
            }
            is Three -> {
                show(state.data)
            }
        }
    }

    private fun show(data: String) {
        Log.d("Response", data)
    }
}

MainViewModel.kt

abstract class MainViewModel : ViewModel() {

    sealed class State {
        data class One(val data: String) : State()
        data class Two(val data: String) : State()
        data class Three(val data: String) : State()
    }

    abstract fun state(): LiveData<State>

    abstract fun fetchFirstThree()
}

MainViewModelImpl.kt

class MainViewModelImpl : MainViewModel() {

    private val stateLiveData: MediatorLiveData<State> = MediatorLiveData()

    override fun state(): LiveData<State> = stateLiveData

    override fun fetchFirstThree() {
        stateLiveData.value = State.One("One")
        stateLiveData.value = State.Two("Two")
        stateLiveData.value = State.Three("Three")
    }
}

Expected output:

Response: One
Response: Two
Response: Three

Actual Output:

Response: Three

As per the output above, the Observer is not being called for the first two values.

Christopherchristopherso answered 29/5, 2018 at 10:58 Comment(2)
Try using MutableLiveData<State> in place of MediatorLiveDataJeremiahjeremias
Same result. The output is still Response: ThreeChristopherchristopherso
W
6

I did some science, re-implementing LiveData and MutableLiveData to log out some data.

Check the source code here.

setValue value=Test1
dispatchingValue mDispatchingValue=false mDispatchInvalidated=false
considerNotify
Returned at !observer.active
setValue value=Test2
dispatchingValue mDispatchingValue=false mDispatchInvalidated=false
considerNotify
Returned at !observer.active
setValue value=Test3
dispatchingValue mDispatchingValue=false mDispatchInvalidated=false
considerNotify
Returned at !observer.active
dispatchingValue mDispatchingValue=false mDispatchInvalidated=false
considerNotify
ITEM: Test3

It looks like the observer hasn't reached an active state when you send the initial values.

private void considerNotify(LifecycleBoundObserver observer) {
    // <-- Three times it fails here. This means that your observer wasn't ready for any of them.
    if (!observer.active) {
        return;
    }

Once the observer reaches an active state, it sends the last set value.

void activeStateChanged(boolean newActive) {
    if (newActive == active) {
        return;
    }
    active = newActive;
    boolean wasInactive = LiveData.this.mActiveCount == 0;
    LiveData.this.mActiveCount += active ? 1 : -1;
    if (wasInactive && active) {
        onActive();
    }
    if (LiveData.this.mActiveCount == 0 && !active) {
        onInactive();
    }
    if (active) {
        // <--- At this point you are getting a call to your observer!
        dispatchingValue(this);
    }
}
Wonderwork answered 29/5, 2018 at 12:41 Comment(6)
How do we change our code, in that case, to make the observer active and receive all the values emitted and not just the last one?Christopherchristopherso
Have you tried running the code in onStart() or onResume()?Wonderwork
If we make the call viewModel.fetchFirstThree() in onStart(), it is still observing the last updated value. However, if we do the same in onResume(), it is being called for all the three values as we are expecting.Christopherchristopherso
There you are. Looks like this is the case for changing state: LiveData considers an observer, which is represented by the Observer class, to be in an active state if its lifecycle is in the STARTED or RESUMED state.Wonderwork
Saved my day. Thank youBarfuss
If you want to retain all emitted values and not just the last one, then use a custom lifecycle aware class and not LiveData.Hecker
H
6

I had such issue too.

To resolve it was created custom MutableLiveData, that contains a queue of posted values and will notify observer for each value.

You can use it the same way as usual MutableLiveData.

open class MultipleLiveEvent<T> : MutableLiveData<T>() {
    private val mPending = AtomicBoolean(false)
    private val values: Queue<T> = LinkedList()

    @MainThread
    override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
        if (hasActiveObservers()) {
            Log.w(this::class.java.name, "Multiple observers registered but only one will be notified of changes.")
        }
        // Observe the internal MutableLiveData
        super.observe(owner, { t: T ->
            if (mPending.compareAndSet(true, false)) {
                observer.onChanged(t)
                //call next value processing if have such
                if (values.isNotEmpty())
                    pollValue()
            }
        })
    }

    override fun postValue(value: T) {
        values.add(value)
        pollValue()
    }

    private fun pollValue() {
        value = values.poll()
    }

    @MainThread
    override fun setValue(t: T?) {
        mPending.set(true)
        super.setValue(t)
    }

    /**
     * Used for cases where T is Void, to make calls cleaner.
     */
    @Suppress("unused")
    @MainThread
    fun call() {
        value = null
    }
}
Handhold answered 4/9, 2020 at 14:39 Comment(0)
T
5

You could use custom LiveData like this:

class ActiveMutableLiveData<T> : MutableLiveData<T>() {

  private val values: Queue<T> = LinkedList()

  private var isActive: Boolean = false

  override fun onActive() {
      isActive = true
      while (values.isNotEmpty()) {
          setValue(values.poll())
      }
  }

  override fun onInactive() {
      isActive = false
  }

  override fun setValue(value: T) {
      if (isActive) {
          super.setValue(value)
      } else {
          values.add(value)
      }
  }
}
Thetic answered 10/10, 2018 at 17:8 Comment(0)
P
3

FWIW I had the same problem but solved it like this...

I originally had some code similar to this...

private fun updateMonth(month: Int){
updateMonth.value = UpdateMonth(month, getDaysOfMonth(month))
}

updateMonth(1)
updateMonth(2)
updateMonth(3)

I experienced the same problem as described... But when I made this simple change....

 private fun updateMonth(month: Int) {
        CoroutineScope(Dispatchers.Main).launch {
            updateMonth.value = UpdateMonth(month, getDaysOfMonth(month))
        }
    }

Presumably, each updateMonth is going onto a different thread now, so all of the updates are observed.

Puleo answered 7/7, 2020 at 15:49 Comment(1)
This simple solution worked for me instead of .postValue()Ordinary
H
1

You should call viewModel.fetchFirstThree() after Activity's onStart() method. for example in onResume() method.

Because in LiveData the Observer is wrapped as a LifecycleBoundObserver. The field mActive set to true after onStart().

class LifecycleBoundObserver extends ObserverWrapper implements GenericLifecycleObserver {

    @Override
    boolean shouldBeActive() {
        return mOwner.getLifecycle().getCurrentState().isAtLeast(STARTED);// return true after onStart()
    }
    @Override
    public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
        if (mOwner.getLifecycle().getCurrentState() == DESTROYED) {
            removeObserver(mObserver);
            return;
        }
        activeStateChanged(shouldBeActive());// after onStart() change mActive to true
    }
}

When the observer notify the change it calls considerNotify, before onStart it will return at !observer.mActive

 private void considerNotify(ObserverWrapper observer) {
    if (!observer.mActive) {// called in onCreate() will return here.
        return;
    }
    if (!observer.shouldBeActive()) {
        observer.activeStateChanged(false);
        return;
    }
    if (observer.mLastVersion >= mVersion) {
        return;
    }
    observer.mLastVersion = mVersion;
    //noinspection unchecked
    observer.mObserver.onChanged((T) mData);
}
Heredia answered 10/10, 2018 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.