Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple calls to set LiveData is not observed

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.

like image 658
mhtmalpani Avatar asked May 29 '18 10:05

mhtmalpani


People also ask

How do you observe LiveData?

Attach the Observer object to the LiveData object using the observe() method. The observe() method takes a LifecycleOwner object. This subscribes the Observer object to the LiveData object so that it is notified of changes. You usually attach the Observer object in a UI controller, such as an activity or fragment.

How do I stop observe LiveData in fragment?

You should manually call removeObserver(Observer) to stop observing this LiveData. While LiveData has one of such observers, it will be considered as active.


5 Answers

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);
    }
}
like image 86
Knossos Avatar answered Oct 10 '22 04:10

Knossos


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
    }
}
like image 44
Ondinnonk Avatar answered Oct 10 '22 05:10

Ondinnonk


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)
      }
  }
}
like image 37
Dmitry Baryshev Avatar answered Oct 10 '22 06:10

Dmitry Baryshev


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.

like image 25
Mark Sheekey Avatar answered Oct 10 '22 06:10

Mark Sheekey


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);
}
like image 43
Davion Avatar answered Oct 10 '22 04:10

Davion