Kotlin,Android,如何正确调试协程? [英] Kotlin, Android, how to debug coroutines correctly?

查看:62
本文介绍了Kotlin,Android,如何正确调试协程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试我的协程,但放置在挂起函数中的断点不起作用.请帮助我理解原因.

使用 Android Studio.

好的,我从 viewModelScope 启动一个协程:

 viewModelScope.launch(IO) {when(val 结果 = interactor.getAllWords()){...}}

getAllWords() 中我写道:

 override suspend fun getAllWords(): WordResult {val words = mutableListOf()当 (val wordsResult = getAllWordsWithoutFiltersApplying()) {}...返回 getWordsWithSelectedPattern()

我有两个挂起函数:getAllWordsWithoutFiltersApplying()getWordsWithSelectedPattern().我在它们两个中都有一个断点,但它们没有在调试模式下触发.

同时,当我将断点置于其行时,行 val words = mutableListOf() 正在触发.

而且,如果我将一些日志内容放入untracing"功能,他们将工作.我这么说是为了说明,暂停功能有效.断点不是.

我应该怎么做才能调试它们?

*截图已添加.看左边有一排图标.为什么我的线路不可用?

解决方案

根据您的示例代码,您在 MAINIO 之间切换协程上下文,因此当您设置断点,确保 suspend 选项是 ALL

显示断点的选项.用鼠标左键单击设置断点,然后在断点上单击鼠标右键.

如果您使用的是 JetBrain IDE,根据

更多细节,您可以查看

I'm trying to debug my coroutines, and breakpoints placed into suspend function don't work. Pls help me understand why.

Working with Android Studio.

Ok, I launch a coroutine from viewModelScope:

    viewModelScope.launch(IO) {
        when(val result = interactor.getAllWords()){...}
    }

In getAllWords() I wrote:

    override suspend fun getAllWords(): WordResult {
        val words = mutableListOf<Word>()

        when (val wordsResult = getAllWordsWithoutFiltersApplying()) {}

        ...

        return getWordsWithSelectedPattern()

I have two suspend functions: getAllWordsWithoutFiltersApplying() and getWordsWithSelectedPattern(). I have a breakpoints into both of them, but they did't trigger in debug mode.

At the same time, line val words = mutableListOf<Word>() is triggering, when I put breakpoint to its line.

And, if I put some log stuff into "untracing" function, they will be work. I say it to make it clear, suspend function works. Breakpoints are not.

What should I do to debug them?

*Screenshot added. Look at the left side with row of icons. Why my lines are not available?

解决方案

Based on your sample code, you switch the coroutine context between MAIN and IO so when you set the breakpoint, make sure the suspend option is ALL

To show the option of the breakpoint. Set a breakpoint with the left click of your mouse, and then right click your mouse on the breakpoint.

If you are using the JetBrain IDE, according to the document, when you set the breakpoint to make sure the suspend option is ALL not thread. it works for me.

and more detail you can check the document

这篇关于Kotlin,Android,如何正确调试协程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆