Kotlin 协程 supervisorScope {} 运行崩溃解决

前言

简单介绍supervisorScope函数,它用于创建一个使用了 SupervisorJob 的 coroutineScope,
该作用域的特点:抛出的异常,不会 连锁取消 同级协程和父协程。

看过很多 supervisorScope {} 文档的使用,我照抄一摸一样的代码,运行就崩溃,最后找到了解决方法,应该是kotlin版本更新做过改动,当前我使用的是 androidx.core:core-ktx:1.9.0

解决方法

需要将CoroutineExceptionHandler,作为参数,才有效果,不然会崩溃。

    private fun test() {

        // 原来的写法,现在会崩溃
//        runBlocking {
//            Log.d("TAG", "Start")
//            launch {
//                delay(100)
//                Log.d("TAG", "Task from runBlocking")
//            }
//            supervisorScope {
//                val firstChild = launch {
//                    Log.d("TAG", "First Child")
//                    throw AssertionError("First child is cancelled")
//                }
//                val secondChild = launch {
//                    Log.d("TAG", "Second Child")
//                }
//                Log.d("TAG", "Cancelling supervisor")
//            }
//            Log.d("TAG", "End")
//        }


        // 最新的写法
        runBlocking {
            Log.d("TAG", "Start")
            launch {
                delay(100)
                Log.d("TAG", "Task from runBlocking")
            }
            supervisorScope {
                // 需要将CoroutineExceptionHandler,作为参数,才有效果,不然会崩溃
                val firstChild = launch(CoroutineExceptionHandler { _, _ -> }) {
                    Log.d("TAG", "First Child")
                    throw AssertionError("First child is cancelled")
                }
                val secondChild = launch {
                    Log.d("TAG", "Second Child")
                }
                Log.d("TAG", "Cancelling supervisor")
            }
            Log.d("TAG", "End")
        }

    }

相关推荐

  1. Kotlin supervisorScope {} 运行崩溃解决

    2024-01-10 08:24:03       25 阅读
  2. Kotlin

    2024-01-10 08:24:03       39 阅读
  3. Kotlin SharingStarted

    2024-01-10 08:24:03       37 阅读
  4. kotlin学习总结

    2024-01-10 08:24:03       32 阅读
  5. 快速入门Kotlin

    2024-01-10 08:24:03       18 阅读
  6. kotlin相关

    2024-01-10 08:24:03       13 阅读
  7. Kotlin->Kotlin作用域

    2024-01-10 08:24:03       14 阅读
  8. Kotlin : Coroutines —简单应用

    2024-01-10 08:24:03       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 08:24:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 08:24:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 08:24:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 08:24:03       20 阅读

热门阅读

  1. 策略模式--在SpringBoot中的使用

    2024-01-10 08:24:03       31 阅读
  2. img标签的奇怪问题

    2024-01-10 08:24:03       34 阅读
  3. js解决pdf使用iframe打印报跨域错误问题

    2024-01-10 08:24:03       34 阅读
  4. vue element plus Button 按钮

    2024-01-10 08:24:03       49 阅读
  5. python&numpy十二: 使用numpy完成图像处理

    2024-01-10 08:24:03       38 阅读
  6. IOC与DI思想

    2024-01-10 08:24:03       37 阅读
  7. 医疗器械分类及是否需要临床

    2024-01-10 08:24:03       36 阅读
  8. 前端项目由nginx迁移到apache httpd

    2024-01-10 08:24:03       38 阅读
  9. Leetcode 1367. Linked List in Binary Tree (二叉树好题)

    2024-01-10 08:24:03       31 阅读
  10. 笔记:ubuntu22.04重启后无法启动网络

    2024-01-10 08:24:03       35 阅读
  11. nacos和openFeign

    2024-01-10 08:24:03       23 阅读
  12. docker 安装redis集群

    2024-01-10 08:24:03       38 阅读
  13. CPU控制的独立式键盘扫描实验

    2024-01-10 08:24:03       28 阅读
  14. Qt UI框架和Duilib UI框架差别

    2024-01-10 08:24:03       35 阅读
  15. 7个Linux搜索和过滤命令

    2024-01-10 08:24:03       35 阅读
  16. C++ 中关键字 Static

    2024-01-10 08:24:03       41 阅读