Android MediatorLiveData源订阅不触发

标签 android android-livedata mutablelivedata

在我的项目中,我使用了稍微修改过的存储库模式:

  • 数据源(例如 API、数据库)。提供实体的CRUD
  • 处理数据源协调的特定数据(例如 UserRepository、SettingsRepository)的存储库(例如,通过 API 调用更新数据库)。提供 CRUD 之上的基本功能
  • ViewModel 使用存储库并创建存储库调用的流(例如,使用 UserRepository 同步用户数据,然后使用 SettingsRepository 同步用户设置)
  • View 是数据绑定(bind)的

在我的存储库中,我使用公开的 LiveData<*> 字段来传达状态 - 例如表示 UserRepository 将有一个公共(public)类型 LiveData 的 currentUser 字段,私有(private) MediatorLiveData,并且它将链接到一个私有(private)字段,该字段包含要检索的当前用户 ID。

但是,出于某种原因,这些订阅(使用 MediatorLiveData 的 addSource() {} 方法)不会触发。

一个几乎 1:1 的例子(由于 NDA 替换了模型名称)如下:

abstract class BaseRepository: ViewModel(), KoinComponent {

    val isLoading: LiveData<Boolean> = MutableLiveData<Boolean>().apply { postValue(false) }

}


class UserRepository: BaseRepository() {

    private val client: IClient by inject() // Koin injection of API client
    private val sharedPref: SharedPrefManager by inject() // custom wrapper around SharedPreferences

    private val currentUserId = MutableLiveData()

    val currentUser: LiveData<User> = MediatorLiveData()
    val users: LiveData<List<User>> = MutableLiveData()

    init {
        (currentUser as MediatorLiveData).addSource(currentUserId) { updateCurrentUser() }
        (currentUser as MediatorLiveData).addSource(users) { updateCurrentUser() }

        (currentUserId as MutableLiveData).postValue(sharedPref.getCurrentUserId())
        // sharedPref.getCurrentUserId() will return UUID? - null if 
    }

    fun updateCurrentUser() {
        // Here I have the logic deciding which user to push into `currentUser` based on the list of users, and if there's a `currentUserId` present.
    }
}

实现此示例后,updateCurrentUser() 永远不会被调用,即使对其他 LiveData 字段的订阅发生并且在调试 currentUser 对象时可见。

通过 addSource 进行的相同订阅在其他存储库中工作得很好,它们的构建方式与上述方式完全相同。

这里可能出了什么问题?

最佳答案

MediatorLiveData 不会观察源 LiveData 如果没有任何观察者订阅它自己。 updateCurrentUser() 将在您订阅 currentUser 后立即调用。

关于Android MediatorLiveData源订阅不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56707851/

相关文章:

android - Jetpack 根据大小组成具有自适应列数的网格

android - 这是 LiveData 的正确使用方式吗?

android - Kotlin更新MutableLiveData的值

java - 在Eclipse ADT中创建android项目后,出现大量错误

Java - 如果无线网络已经存在,如何连接到无线网络?

android - 为什么当我导航回 fragment 时会调用 onChanged?

android - Kotlin中MutableLiveData中的泛型类型

android - Android RecyclerView SubmitList无法在LiveData观察器中更新数据

Android Studio : link to external module?

android - LiveData 分页列表大小始终为 0