当前位置:网站首页>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 .
边栏推荐
- Static analysis of malicious code
- STM32 USB Basics
- Converting Chinese numbers to Arabic numbers in Delphi
- Solution to the problem of closing the watchdog of STM32 in the sleep mode (stop/standby)
- What is the difference between pytorch and tensorflow?
- AOF持久化
- MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
- A simple deadlock example
- Mongodb array operation
- Aof persistence
猜你喜欢
随机推荐
Programming training 1
OceanBase 雄踞墨天轮2021年度中国数据库魔力象限领导者
[MRCTF2020]Ez_ bypass --BUUCTF
什么是 dummy change?
Canvas airplane game
Buuctf's babysql[geek challenge 2019]
RCC clock configuration of stm32f401
[virtual machine] notes on virtual machine environment problems
[imx6ull] video monitoring project (USB camera +ffmepeg)
Assembly language learning
STM32 USB Basics
【服务器数据恢复】存储服务器之间迁移数据时数据丢失恢复成功案例
(01).NET MAUI实战 建项目
antdPro - ProTable 实现两个选择框联动效果
Aunt learning code sequel: ability to sling a large number of programmers
Biological unlocking - Fingerprint entry process
Five mock technologies of go
硬(磁)盘(二)
pytorch是什么?解释pytorch的基本概念
Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not

![[GYCTF2020]Ezsqli --BUUCTF](/img/8b/3c8b48daf7719482a235fd622737aa.png)





![[imx6ull] video monitoring project (USB camera +ffmepeg)](/img/f9/1a7b68083b52c973336db9044b52c2.jpg)
