androidx.paging.DataSource.Factory时,房间无法检索数据(使用LiveData + Kotlin协程)

时间:2019-03-01 14:54:59

标签: android android-room android-architecture-components android-livedata kotlin-coroutines

我有一个AmbassadorDAO,其中的getAll() : List<Ambassador>可以正确返回大使列表。

当我重构现有代码以使用DataSource.Factory来分页列表时,问题就变成了

这是代码

演示模块

活动

class AmbassadorActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        ...

        val viewModel by viewModel<AmbassadorViewModel>()

        val adapter = AmbassadorAdapter(this)
        list_of_ambassadors.adapter = adapter

        viewModel.ambassadors.observe(this, Observer { adapter.submitList(it) })

        viewModel.listAmbassadors()

        ...
    }

    ...
}

视图模型

class AmbassadorViewModel(
    ...,
    private val getAllAmbassadorInteractor: GetAllAmbassadorInteractor
) : ViewModel() {

    ...

    // not working
    private val _ambassadors = MutableLiveData<PagedList<Ambassador>>()

    // it's working
    //private val _ambassadors = MutableLiveData<List<Ambassador>>()

    ...

    // not working
    val ambassadors : LiveData<PagedList<Ambassador>>
        get() =  _ambassadors

    // it's working
    //val ambassadors : LiveData<List<Ambassador>>
    //    get() =  _ambassadors

    ...

    fun listAmbassadors() {
        viewModelScope.launch {
            try {
                ...

                // not working
                // the data not return anything
                // the livedata is notified with null
                val data = getAllAmbassadorInteractor.exec()
                _ambassadors.value = LivePagedListBuilder(data, 20).build().value

                // it's working
                //_ambassadors.value = getAllAmbassadorInteractor.exec()

            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                ...
            }
        }
    }

}

域模块

PRESENTATION(我的用例界面)之间的边界

interface GetAllAmbassadorInteractor {

    //suspend fun exec() : List<Ambassador>
    suspend fun exec() : DataSource.Factory<Int, Ambassador>

}

用例实现

class GetAllAmbassadorInteractorImpl(
    private val repository: AmbassadorRepository
) : GetAllAmbassadorInteractor {

    override suspend fun exec() = withContext(Dispatchers.IO) { repository.getAll() }

}

数据之间的边界(我的存储库界面)

interface AmbassadorRepository {

    ...

    //suspend fun getAll() : List<Ambassador>
    suspend fun getAll() : DataSource.Factory<Int, Ambassador>

    ...

}

数据模块

存储库实施

class AmbassadorRepositoryImpl(
    private val ambassadorDAO: AmbassadorDAO
) : AmbassadorRepository {

    ...

    override suspend fun getAll() = ambassadorDAO.getAll().map { it.toDomain() }

    ...

}

我的DAO

@Dao
interface AmbassadorDAO {

    ...

    @Query("SELECT * FROM ${AmbassadorEntity.TABLE_NAME} ORDER BY name DESC")
    fun getAll(): DataSource.Factory<Int, AmbassadorEntity>
    //fun getAll(): List<AmbassadorEntity>

    ...

}

我在哪里错了?

2 个答案:

答案 0 :(得分:1)

借助Kotlin扩展(LifecycleScope)的支持,我们可以轻松地将LiveData与Coroutine连接起来,并且您无需使用_ambassadors之类的支持属性并将其设置为MutableLiveData。

androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01 or higher.

就像这是一个使用协程并返回LiveData的函数

/**
 * Get all news rows livedata pageList from DB using Coroutine.
 */
suspend fun getAllNewsLiveData(): LiveData<PagedList<News>> {
    return withContext(Dispatchers.IO) {
        val data = mDao.getAllNews()
        LivePagedListBuilder(data, Constants.PAGINATION_SIZE).build()
    }

}

现在在UI类中,我们可以使用Lifescope扩展名简单地调用此函数

  lifecycleScope.launchWhenStarted {
        newsViewModel.getNews()?.observe(this@NewsActivity, Observer { pagedNewsList -> pagedNewsList.let { newsAdapter.submitList(pagedNewsList) } })
    }

答案 1 :(得分:0)

我猜你的错误是在AmbassadorViewModel类的这一行上的:

_ambassadors.value = LivePagedListBuilder(data, 20).build().value

代替该用法:

_ambassadors.value = LivePagedListBuilder(data, 20).build()

也请参阅此post,也许会有所帮助。