0

In my ViewModel I have such a code:

    ...
    private val billingClientLifecycle: BillingClientLifecycle
    private val _isBillingConnectionReady = MutableLiveData<Boolean>()
    val isBillingConnectionReady: LiveData<Boolean> = _isBillingConnectionReady
    ...
    
    init {
        ...
        billingClientLifecycle.setPurchaseUpdateListener(
            object : IapPurchasesUpdatedListener {
                ...

                override fun isBillingConnected(state: Boolean) {
                    Log.i(TAG, "Billing connection state is: $state")
                    _isBillingConnectionReady.value = state
                }
            }
        )

        billingClientLifecycle.createBillingConnection(getApplication())
        ...
    }
    
    ...

So here I have a billingClientLifecycle object and in init() I invoke createBillingConnection method I see that I get a response in the callback isBillingConnected and I see that _isBillingConnectionReady livedata is being called, however, I don't get this event in my fragment where I subscribed on this event.

What am I missing here? Why live data doesn't pass the event? Could it be somehow because the callback captures the values?

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121

1 Answers1

0

Actually I found a solution, the problem is here, to be honest I totaly forgot about these methods diff.

Link to the orig SO post :

Based on the documentation:

setValue():

Sets the value. If there are active observers, the value will be dispatched to them. This method must be called from the main thread.

postValue():

Posts a task to a main thread to set the given value. If you called this method multiple times before a main thread executed a posted task, only the last value would be dispatched.

To summarize, the key difference would be:

setValue() method must be called from the main thread. But if you need set a value from a background thread, postValue() should be used.

Sirop4ik
  • 4,543
  • 2
  • 54
  • 121
  • I don't see why this is relevant, since you are setting the value from the main thread, the IapPurchasesUpdatedListener. – Tenfour04 Nov 02 '22 at 19:10