1

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)
Olinasc
  • 1,261
  • 1
  • 12
  • 20
  • can you add code snippet having `signInViewModel.googleApiClient.value = it` line? – Sachin Chandil Jun 21 '17 at 04:51
  • Sure... it is a RxJava subscription. there is nothing especial about it, but here it goes (also emphasizing I've debugged it and the callback is called. Inside LiveData, the Observer is not considered active and therefore is not called. – Olinasc Jun 21 '17 at 16:33
  • See you are having problem with flow of your code in `SignInViewModel` class. You need to post whole `SignInViewModel` class. if you can not put all code then remove your business login in method but keep methods flow. With the info you have given no one can give a solution. – Sachin Chandil Jun 21 '17 at 16:57
  • I've reproduced the error with the whole class you see in the post. Sorry but `SignInViewModel` is complete. – Olinasc Jun 21 '17 at 21:21
  • @Olinasc Just curious to know how are you able to inject billingApi without using any ViewModel Factory – hiten pannu Apr 10 '18 at 10:49
  • There is a factory. I just removed it for avoiding discussing the fact. It follows the Github sample from google samples. – Olinasc Apr 11 '18 at 20:51

1 Answers1

1

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>(...);
}
Olinasc
  • 1,261
  • 1
  • 12
  • 20