【发布时间】:2020-04-13 06:27:49
【问题描述】:
我已经在我的 viewModel 中调用了emit(),但是当我定义Resource 的类型为Artist 时,我不知道为什么我的LiveDataScope 返回Resource<Any>
视图模型
class EventsViewModel(private val useCase: Events):ViewModel() {
val fetchArtistList = liveData(Dispatchers.IO){
try {
val artistList = useCase.getEvents()
emit(artistList)
}catch (e:Exception){
Crashlytics.logException(e.cause)
emit(Resource.error("Error: ",e.message))
}
}
}
用例
class EventsImpl(private val eventsRepo:EventsRepo): Events {
override suspend fun getEvents(): Resource<MutableList<Artist>> = eventsRepo.getEventsDB()
}
回购
class EventsRepoImpl : EventsRepo {
override suspend fun getEventsDB(): Resource<MutableList<Artist>> {
val artistList = mutableListOf<Artist>()
val resultList = FirebaseFirestore.getInstance()
.collection("events")
.get().await()
for (document in resultList) {
val photoUrl = document.getString("photoUrl")
val artistName = document.getString("artistName")
val place = document.getString("place")
val time = document.getString("time")
val day = document.getLong("day")
artistList.add(Artist(photoUrl!!, artistName!!, time!!, place!!, day!!))
}
return Resource.success(artistList)
}
}
但由于某种原因,它没有在我的视图模型中使用 Resource<MutableList<Artist>> 推断类型,而是为 LiveData 提供了 Resource<Any>:
我在另一个类中实现了相同的方法,但 livedata 恢复正常,我尝试清除缓存并重新启动、清理和重建,但它始终返回相同
为什么不能正确推断类型?
【问题讨论】:
标签: android firebase kotlin mvvm android-livedata