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

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

问题描述

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

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

使用Android Studio.

Working with Android Studio.

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

Ok, i launch a coroutine from viewModelScope:

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

我在getAllWords()中写道:

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

        when (val wordsResult = getAllWordsWithoutFiltersApplying()) {}

        ...

        return getWordsWithSelectedPattern()

我有两个挂起函数:getAllWordsWithoutFiltersApplying()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.

同时,当我在断点处插入断点时,行val words = mutableListOf<Word>()正在触发.

At the same time, line val words = mutableListOf<Word>() is triggering, when i put breakpoint to it's line.

而且,如果我将一些日志内容放入"unracing"功能中,它们将起作用.我说的很清楚,暂停功能有效.断点不是.

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.

我应该怎么调试它们?

*已添加屏幕截图.用图标行看左边.为什么我的线路不可用?

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

推荐答案

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

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

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

如果您使用的是JetBrain IDE,请根据文档,当您设置断点以确保suspend选项为ALL不是线程时.它对我有用.

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天全站免登陆