概述
ViewModel 调用数据的方式
@Query("SELECT * FROM pocketbook WHERE record_type = :type AND create_time >= :createTimeStart AND create_time <= :createTimeEnd ORDER BY id DESC")
fun getPocketbookListNew(type: String, createTimeStart: Long, createTimeEnd: Long): Flow<List<Pocketbook>>
1.升级前
fun getPockBookListNew(
type: String,
createTimeStart: Long,
createTimeEnd: Long
): Flow<List<Pocketbook>> {
val cities = db.gePockBookDao().getPocketbookListNew(type, createTimeStart, createTimeEnd)
return cities.catch { e -> e.printStackTrace() }.flowOn(Dispatchers.IO)
}
2.升级后
ViewModel
val loadStateOfPockBookList = MutableLiveData<List<Pocketbook>>()
fun getPockBookListNew1(
type: String,
createTimeStart: Long,
createTimeEnd: Long
) = launch {
val cities = db.gePockBookDao().getPocketbookListNew(type, createTimeStart, createTimeEnd)
cities.catch { e -> e.printStackTrace() }.flowOn(Dispatchers.IO).collect {
loadStateOfPockBookList.postValue(it)
}
}
Activity
noteBookVM.loadStateOfPockBookList.observe {
mAdapter.setNewData(it)
}
@GET("/marketing/consumer/activities/invite")
suspend fun getInvite(): Response<InviteModel>
val loadStateOfInvite = MutableLiveData<InviteModel>()
fun getInvite1() = launch {
flow {
var model= activitiesService.getInvite().data
emit(model)
}.flowOn(Dispatchers.IO).catch { e->e.printStackTrace() }.collect {
loadStateOfInvite.postValue(it)
}
}
最后
以上就是机智小甜瓜为你收集整理的ViewModel 协程 flow 调用Room和网络数据的方式的全部内容,希望文章能够帮你解决ViewModel 协程 flow 调用Room和网络数据的方式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复