问题
I trying to debug my coroutines, and breakpoints placed into suspend function does't work. Pls help me to 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 it's 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?
回答1:
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
回答2:
I know I'm late, but this is for those people who made similar mistakes like me. I've also faced this issue recently and the root cause was related to dependencies. Actually I added coroutine core dependency but I forget to add coroutine android dependency. Please make sure both dependencies are present in your gradle file as shown below.
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.4"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
来源:https://stackoverflow.com/questions/59345012/kotlin-android-how-to-debug-coroutines-correctly