androidx.paging.datasource.factory(使用livedata kotlin corou



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

当我重构我存在的代码使用dataSource.factory fectine我的列表

时,问题就变成了。

这是代码

预售模块

活动

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

ViewModel

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 {
                ...
            }
        }
    }
}

域模块

演示文稿(我的用户程序接口)

之间的边界
interface GetAllAmbassadorInteractor {
    //suspend fun exec() : List<Ambassador>
    suspend fun exec() : DataSource.Factory<Int, Ambassador>
}

USECASE实现

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>
    ...
}

我在哪里错了?

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

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

而不是使用:

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

也请参考此帖子,也许会有所帮助。

在Kotlin Extension(LifeCyclesCope)的支持下,我们可以轻松地将Livedata与Coroutine连接起来,而您无需使用_AMBASSADORS之类的备份属性并将其制成MutableLivedata。

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

这样的功能是使用coroutine并返回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 Extension调用此功能

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

相关内容

  • 没有找到相关文章

最新更新