我有一个 android 应用程序,我想在其中实现 Kotlin Couroutines,我遇到的困惑是我在哪里使用协程。我有 viewModel 作为-
class PostViewModel : ViewModel() {
var postPagedList: LiveData<PagedList<UnsplashImageDetails>>? = null
private var postLiveDataSource: LiveData<PageKeyedDataSource<Int, UnsplashImageDetails>>? = null
var popularPagedList: LiveData<PagedList<UnsplashImageDetails>>? = null
private var popularLiveDataSource: LiveData<PageKeyedDataSource<Int, UnsplashImageDetails>>? = null
init {
val postDataSourceFactory = PostDataSourceFactory()
val popularDataSourceFactory = PopularDataSourceFactory()
postLiveDataSource = postDataSourceFactory.getPostLiveDataSource()
popularLiveDataSource = popularDataSourceFactory.getPopularLiveDataSource()
val config: PagedList.Config = (PagedList.Config.Builder()).setEnablePlaceholders(false)
.setPageSize(PostDataSource().PAGE_SIZE).build()
val configPop: PagedList.Config = (PagedList.Config.Builder()).setEnablePlaceholders(false)
.setPageSize(PopularDataSource().PAGE_SIZE).build()
postPagedList = LivePagedListBuilder(postDataSourceFactory, config).build()
popularPagedList = LivePagedListBuilder(popularDataSourceFactory, configPop).build()
}
}
Run Code Online (Sandbox Code Playgroud)
我应该在哪里使用异步方法,在此活动中或在我通过改造获取数据的存储库类中。