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?
Android Online Course for Professionals by MindOrks Similarly, we can do any type of background tasks in parallel using Kotlin Coroutines.
Running coroutines sequentially or in parallel One of the main advantages of asynchronous programming is that it allows to offload computations to separate threads and run them in parallel in case the result of each task is not needed by the others. They can all run independently making use of multiple CPU cores.
Quoting definition of Global Scope from Kotlin's documentation– “Global scope is used to launch top-level coroutines which are operating on the whole application lifetime and are not cancelled prematurely.” GlobalScope creates global coroutines and these coroutines are not children of some specific scope.
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
It still does not work for me because I run the app first then attach the debugger, but when I use debug instead, it works.
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'
I had forgot minifyEnabled
set to true in my build.gradle
file
buildTypes {
release {
minifyEnabled true
}
debug {
minifyEnabled true <- Should be false for these breakpoints to work
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With