Android LiveData:MutableData永远不会处于活动状态

时间:2017-06-20 22:31:31

标签: android kotlin android-architecture-lifecycle

我有以下ViewModel

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

Activity.onCreate(onSavedInstanceState: Bundle?)我有:

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
    }

稍后我的代码中有signInViewModel.googleApiClient.value = it。此时(在按钮点击后发生,因此我处于恢复状态)我希望LiveData触发我的观察者,但

调试时我注意到我的MutableLiveData永远不会处于active状态。

我做错了什么?请知道我在示例中使用了GoogleApiClient实例,并且应该使用带有自动管理的Activity和whatnot来初始化它,但这不是问题所在。我想动态设置它并让我的观察者被触发。

修改:添加调用setValue的代码

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

1 个答案:

答案 0 :(得分:1)

事实证明minifyEnabled是真的。我还没有看到有关架构组件的proguard规则的任何内容。

发现此issue尚未解决,但具有使其通过所需的配置:

-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>(...);
}