上一页 1 ··· 27 28 29 30 31 32 33 34 35 ··· 181 下一页
摘要: 一,代码: //处理按钮点击事件 binding.button1.setOnClickListener { runBlocking { flow { // 上游,发源地 emit(1) // 挂起函数 emit(2) emit(3) emit(4) emit(5) }.filter { printl 阅读全文
posted @ 2025-08-02 15:00 刘宏缔的架构森林 阅读(11) 评论(0) 推荐(0)
摘要: 一,代码: 例子:需要访问缓存和网络来获取数据,如果从缓存先返回,先更新ui,从网络获取数据后再更新一次,如果从网络先返回,更新ui 数据类: data class Product( //id val productId: String, //价格 val price: Double, // 是不是 阅读全文
posted @ 2025-08-02 15:00 刘宏缔的架构森林 阅读(11) 评论(0) 推荐(0)
摘要: 一,代码: 自定义dispatcher: 自定义了两个dispatcher val myCustomDispatcher1 = newSingleThreadContext("MyCustomThread1") val myCustomDispatcher2= Executors.newSingle 阅读全文
posted @ 2025-08-02 15:00 刘宏缔的架构森林 阅读(59) 评论(0) 推荐(0)
摘要: 一,代码: 函数: //得到用户信息,演示指定dispatcher suspend fun getUserInfo(): String { logX("Before IO Context.") withContext(Dispatchers.IO) { logX("In IO Context.") 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(43) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button2.setOnClickListener { runBlocking { suspend fun getResult1(): String { delay(3000L) // 模拟耗时操作 return "Result1" } suspe 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(8) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button1.setOnClickListener { runBlocking { suspend fun download() { // 模拟下载任务 delay(1000L) } println("得到job") val job = launc 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(19) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button2.setOnClickListener { runBlocking(Dispatchers.IO) { repeat(3) { index -> launch { //println() repeat(3) { println("第"+ 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(46) 评论(0) 推荐(0)
摘要: 一,代码: 函数: //得到sequence fun getSequence() = sequence { println("添加 1") yield(1) println("添加 2") yield(2) println("添加 3") yield(3) println("添加 4") yield 阅读全文
posted @ 2025-08-02 14:59 刘宏缔的架构森林 阅读(26) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button1.setOnClickListener { runBlocking { val channel = Channel<Int>() //发送消息 launch { (1..3).forEach { channel.send(it) log 阅读全文
posted @ 2025-07-26 11:39 刘宏缔的架构森林 阅读(25) 评论(0) 推荐(0)
摘要: 一,代码: //处理按钮点击事件 binding.button1.setOnClickListener { runBlocking { //给协程命名 val job = launch(CoroutineName("协cor1")) { val corName = coroutineContext[ 阅读全文
posted @ 2025-07-26 11:39 刘宏缔的架构森林 阅读(15) 评论(0) 推荐(0)
上一页 1 ··· 27 28 29 30 31 32 33 34 35 ··· 181 下一页