当前位置:网站首页>Kotlin coroutine withcontext switch thread
Kotlin coroutine withcontext switch thread
2022-06-13 00:46:00 【Ango cannot move】
GlobalScope.launch(Dispatchers.Main) {
// Switch to the sub thread for operation
val repoResponse: Response<FeedBackLists>
withContext(Dispatchers.IO) {
repoResponse = RetrofitClient
.instance
.getApi()
//suspend The suspended method must be called in Ctrip
.getFeedBack(1, 1)
}
// Or in the main thread
binding.tvText.text = "repoResponse :${repoResponse.data!!.data[0].content}"
}
withContext You can switch the current thread from the main thread to io Threads . Then switch back to after the execution ui Threads perform operations
The advantage of doing so is to eliminate
Code nesting
Instead, it can be written as a parent-child relationship to facilitate process cooperation
withContent Can also be nested into suspend fun Among the methods
Suspend is a non blocking thread
launch What's inside is Xiecheng .
边栏推荐
- Five mock technologies of go
- Android Weather
- Today's sleep quality record 74 points
- ImportError: cannot import name &#039;get_ora_doc&#039; from partially initialized module
- 杂记:intel11代和12代移动版支持原生Thunderbolt4接口,桌面版不支持
- Hard (magnetic) disk (I)
- 三栏简约typecho主题Lanstar/蓝星typecho主题
- Androi天氣
- Kali system -- dnsmap for DNS collection and analysis
- [buglist] serial port programming does not read data
猜你喜欢
JPA execution failed in scheduled task -executing an update/delete query transactionrequiredexception
Today's sleep quality record 74 points
Influence of higher order poles on waveform
Some basic design knowledge
[CISCN2019 华北赛区 Day2 Web1]Hack World --BUUCTF
高阶极点对于波形的影响
硬(磁)盘(二)
Aof persistence
深度学习每周期的步数多少合适?
Mysql批量插入数据时如何解决重复问题?
随机推荐
kotlin 协程withContext切换线程
Canvas game lower level 100
People and gods are angry. Details of Tangshan "mass beating of women incident"
Kotlin 协程的作用域构建器 coroutineScope与runBlocking 与supervisorScope,协程同步运行,协程挂掉的时候其他协程如何不被挂掉。
Converting Chinese numbers to Arabic numbers in Delphi
Leetcode weekly -- April to May
从ADK的WinPE自己手动构建自己的PE
Canvas airplane game
Druid reports an error connection holder is null
ROS2之OpenCV人脸识别foxy~galactic~humble
pytorch和tensorflow有什么区别?
RCC clock configuration of stm32f401
Androi天气
How many steps are appropriate for each cycle of deep learning?
Win10 home vs pro vs enterprise vs enterprise LTSC
Mongodb array operation
1. Google grpc framework source code analysis Hello World
[virtual machine] notes on virtual machine environment problems
Biological unlocking - Fingerprint entry process
Opencv face recognition of ros2 foxy~galactic~humble