我的代码
fun weatherByCity(cityName: String): LiveData<Resource<Response>> = liveData(Dispatchers.IO) {
val response = appRepository.weatherByCity(cityName)
emit(response)
}
fun weatherByZipCode(zipCode: String): LiveData<Resource<Response>> = liveData(Dispatchers.IO) {
val response = appRepository.weatherByZipCode(zipCode)
emit(response)
}
fun weatherByLocation(latLong: String): LiveData<Resource<Response>> = liveData(Dispatchers.IO) {
val response = appRepository.weatherByLocation(lat?:"", long?:"")
emit(response)
}
这里我使用了三个 liveData{} block ,它们从存储库中获取数据并进行改造。但它们具有相同的返回类型 LiveData<Resource<Response>>
.我可以对所有三个使用单个 liveData{},如果可以,那么该怎么做?还有一个问题,使用 liveData(Dispatchers.IO) 或使用 liveData{}(没有 Dispatchers.IO)会有什么不同吗?
最佳答案
Using liveData(Dispatchers.IO) or using liveData{} (without Dispatchers.IO) will make any difference?
是的,默认情况下 liveData{}
构建器将在 UI(Main) 线程上调用,但由于您已明确添加liveData(Dispatchers.IO){}
现在将安排在 IO 线程上。
我个人所做的是,让 liveData{}
像往常一样在 UI 线程上运行,但在 上调用我所有的 suspend
函数IO 线程,只是为了确保 emit()
操作在 UI 线程上执行,没有任何延迟。例如:
val someData = liveData{
val dataFromNetwork = getDataFromNetwork()
emit(dataFromNetwork)
}
suspend fun getDataFromNetwork() = withContext(Dispatchers.IO){
apiClient.getData()
}
Can I use single liveData{} for all three, if yes then how to do that?
嗯,这很难说,因为这是设计选择的问题,并且会因主题而异,还取决于您如何使用所有三个函数的结果(这三个方法是按顺序调用还是并行调用) )。不过,我会尽力给一个方向,
假设你想出了这样的东西,
fun getWeatherData(criteriaToGetWeatherData:String) = liveData(Dispatchers.IO){
when(criteriaToGetWeatherData){
isByCity -> {
val response = appRepository.weatherByCity(cityName)
emit(response)
}
isByZipCode -> {
val response = appRepository.weatherByZipCode(zipCode)
emit(response)
}
isByLocation -> {
val response = appRepository.weatherByLocation(lat?:"", long?:"")
emit(response)
}
}
}
您现在已将三个函数的样板代码简化为一个函数。
但请考虑以下事项,
- 现在您的单个函数可以完成三个函数的工作。
- 您的功能已经很庞大, future 还有更多的扩展范围。
- 您违反了打开/关闭原则,您的功能将被修改 每当 future 出现新的天气标准时。
- 最糟糕的是,所有
repository
方法都是异步函数,假设这三个方法都被快速连续调用,则无法保证也无法区分哪个先返回,哪个emit()
有哪些天气数据。此外,如果您只有一个订阅者观察getWeatherData()
,您将不知道您收到通知的三个内部方法中的哪一个。
根据 Bob Martin 叔叔的 Clean Code:
Functions should do one thing. They should do it well, They should do it only.
归根结底,我们必须记住代码可读性是终极之王。所以遵循KISS(保持简单愚蠢)的原则,我会保留这三个函数。
关于android - 在 Dispatchers.IO 线程上使用 liveData{} 协程构建器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60678290/