Olinasc
Olinasc

Reputation: 1291

Android LiveData: MutableData is never on active state

I have the following ViewModel:

class SignInViewModel @Inject constructor(val api: BillingApi) : ViewModel() {
    val googleApiClient: MutableLiveData<GoogleApiClient> = MutableLiveData()
}

On my Activity.onCreate(onSavedInstanceState: Bundle?) I have:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    ...
    signInViewModel = ViewModelProviders.of(this)
            .get(SignInViewModel::class.java)

    signInViewModel.googleApiClient.observe(this, Observer<GoogleApiClient?> {
        ... // here never gets trigged
    }

Later on my code I have signInViewModel.googleApiClient.value = it. At this point (which happens after a button click, so I am in a resumed state) the I expected the LiveData to trigger my observer, but it does not.

While debugging I've noticed that my MutableLiveData is never on an active state.

What am I doing wrong? Please, I know I am using a GoogleApiClient instance in the example and that it should be initialized with the Activity with automanage and whatnot, but this is not the issue here. I want to set it dynamically and have my observer be triggered.

Edit: adding the code that calls the setValue

signInViewModel.someMethod(this)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(Consumer {
                // This gets called but the observe callback does **not**
                signInViewModel.googleApiClient.value = it
            }, errorCallback)

Upvotes: 1

Views: 1347

Answers (1)

Olinasc
Olinasc

Reputation: 1291

It turns out minifyEnabled was true. I haven't seen anything about proguard rules for architecture components.

Found this issue which is not resolved yet but has the configuration needed to make it pass:

-keepclassmembers class * implements android.arch.lifecycle.LifecycleObserver {
    <init>(...);
}
-keepclassmembers class * extends android.arch.lifecycle.ViewModel {
    <init>(...);
}
-keepclassmembers class android.arch.lifecycle.Lifecycle$State { *; }
-keepclassmembers class android.arch.lifecycle.Lifecycle$Event { *; }
-keepclassmembers class * {
    @android.arch.lifecycle.OnLifecycleEvent *;
}

-keep class * implements android.arch.lifecycle.LifecycleObserver {
    <init>(...);
}

Upvotes: 1

Related Questions