Nin*_*ski 6 android mvvm coroutine kotlin
我对 ViewModel 中的协程感到困惑。
我的问题很简单:为什么下面的协程看起来没有阻止 UIThread?(协程运行时UI依然流畅)
我的片段就在这里:
class FragmentSeePaths : Fragment(R.layout.fragment_see_paths),
PathRecyclerAdapter.OnSetPathForWidgetListener {
private val pathViewModel: PathViewModel by activityViewModels()
private lateinit var binding: FragmentSeePathsBinding
private lateinit var listener: OnAddLineRequestListener
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
...
}
private fun observeWidgetPath() {
pathViewModel.getUserWidgetPath().observe(viewLifecycleOwner, Observer {
if (it != null) {
lifecycleScope.launch {
val times = pathViewModel.fetchBusTime(it)
updateUI(it, times)
}
}
})
}
Run Code Online (Sandbox Code Playgroud)
这是使用 fetchBusTime 方法拍摄的 ViewModel:
suspend fun fetchBusTime(path: Path): Pair<List<Time>?, List<Time>?> {
Log.v("fetchBusTimeUI", Thread.currentThread().name) // Main
// Some network requests made with Retrofit
val timesResponseStartPoint: GinkoTimesResponse? = repository.getTimes(
path.startingPoint.startName,
path.line.lineId,
path.isStartPointNaturalWay
)
val timesResponseEndPoint: GinkoTimesResponse? = repository.getTimes(
path.endingPoint.endName,
path.line.lineId,
path.isStartPointNaturalWay
)
return timesResponseStartPoint to timesResponseEndPoint
}
Run Code Online (Sandbox Code Playgroud)
launch
允许我们在后台启动协程并同时继续工作。Suspending
函数可以挂起当前协程的执行而不阻塞当前线程。我们可以在以下任何调度程序下启动协程。
为了详细解释你,我从文档中举了一个例子:-
\nfun main() { \n GlobalScope.launch { // launch new coroutine in background and continue\n delay(1000L)\n println("World!")\n }\n println("Hello,") // main thread continues here immediately\n runBlocking { // but this expression blocks the main thread\n delay(2000L) // ... while we delay for 2 seconds to keep JVM alive\n } \n}\n
Run Code Online (Sandbox Code Playgroud)\n评论应该不言而喻。这将立即打印 \xe2\x80\x9cHello,\xe2\x80\x9d,并在一秒钟后添加 \xe2\x80\x9cWorld!\xe2\x80\x9d。\n这与你的代码相同,挂起函数fetchBusTime()
将在不阻塞线程的情况下执行,并且在该方法内的操作完成后,它将执行updateUI(it, times)
。
有关这方面的更多详细信息,请在此处阅读这篇文章
\n 归档时间: |
|
查看次数: |
2985 次 |
最近记录: |