//catch
throw NullPointerException()
}
.onStart {
Log.e(TAG, "onStart: ")
showLoading(“loading…”)
}
.onCompletion {
Log.e(TAG, "onCompletion: ")
dismissLoading()
}
.catch {
Log.e(TAG, "catch: ")
mBinding.textView.text = “数据出错”
}
.collect {
Log.e(TAG, “collect: $it”)
mBinding.textView.text = “接收数据为:$it”
}
}
flowOn:线程切换(flow为IO,其他为Main)
lifecycleScope.launch {
Log.e(TAG, “flow:${Thread.currentThread()}”)
flow {
Log.e(TAG, “emit start:${Thread.currentThread()}”)
emit(“1”)
Log.e(TAG, “emit 1:${Thread.currentThread()}”)
emit(“2”)
Log.e(TAG, “emit 2:${Thread.currentThread()}”)
emit(“3”)
Log.e(TAG, “emit 3:${Thread.currentThread()}”)
emit(“4”)
Log.e(TAG, “emit 4:${Thread.currentThread()}”)
emit(“5”)
Log.e(TAG, “emit 5:${Thread.currentThread()}”)
emit(“6”)
Log.e(TAG, “emit 6:${Thread.currentThread()}”)
}
.flowOn(Dispatchers.IO)
.onStart {
Log.e(TAG, “onStart:${Thread.currentThread()}”)
showToast(“开始”)
}
.filter {
Log.e(TAG, “filter:${Thread.currentThread()}”)
it != “2”
}
.map {
Log.e(TAG, “map:${Thread.currentThread()}”)
“转换$it”
}
.transform<String,Int>{
Log.e(TAG, “transform1:${Thread.currentThread()}”)
emit( it.lengt