https://kotlinlang.org logo
Title
j

Jason Ankers

02/05/2021, 4:43 PM
launch {
    val result = getSomething().apply {
        doSomethingSuspending()
    }
    // code at this point never runs
}
Could anyone explain why this happens?
w

wasyl

02/05/2021, 4:44 PM
Most likely
doSomethingSuspending()
never completes
👆 1
g

Guillermo Alcantara

02/05/2021, 4:44 PM
If I understand this correctly, doSomethingSuspending never returns
l

louiscad

02/05/2021, 4:45 PM
Your
doSomethingSuspending()
is a fancy version of
awaitCancellation()
Or, your
getSomething()
is that one.
j

Jason Ankers

02/05/2021, 4:47 PM
Code after doSomethingSuspending() inside the apply block executes, but code outside the apply block doesnt. That means it completes right?
w

wasyl

02/05/2021, 4:48 PM
Yes, but it shouldn’t be happening. Could the code after doSomethingSuspending be throwing an exception?
l

louiscad

02/05/2021, 4:49 PM
Your snippet didn't show any code after
doSomethingSuspending()
@Jason Ankers It's kinda cheating! 😛
j

Jason Ankers

02/05/2021, 4:49 PM
Theres no code after it - i just added a print statement to see if it executes
l

louiscad

02/05/2021, 4:50 PM
Are you on Android with AGP 7 alpha?
j

Jason Ankers

02/05/2021, 4:51 PM
Yes
l

louiscad

02/05/2021, 4:52 PM
Try rebuilding the app, and if it still occurs, please try doing a Gradle clean, then building with the
--no-build-cache
option and .
j

Jason Ankers

02/05/2021, 4:53 PM
The backstory is I had some code like this:
launch {
    val result = getSomething()
    result.doSomethingSuspending()
    // do stuff
}
Which I changed to this:
launch {
    val result = getSomething().apply {
        doSomethingSuspending() 
    }
    // do stuff
}
but after making the change,
result
never gets assigned, and any code after it never runs
So in theory that should work fine?
l

louiscad

02/05/2021, 4:55 PM
Yes it should, but please, try what I asked and answer these, I'd like to know if it's an issue in AGP 7 alpha I've been witnessing
j

Jason Ankers

02/05/2021, 4:55 PM
Ok will do
Didn’t work after any of that
l

louiscad

02/05/2021, 5:02 PM
Can you wrap the whole thing with a try catch finally block that logs before rethrowing and in the finally block?
Plus insert a log before all this code to ensure the changes are deployed
j

Jason Ankers

02/05/2021, 5:05 PM
Ok yep the function is throwing something 🙂
l

louiscad

02/05/2021, 5:06 PM
Łukasz Wasylkowski was right the whole time on his first message.
j

Jason Ankers

02/05/2021, 5:11 PM
Yep - apologies for not trying that first. I had just assumed if the function was throwing it wouldn’t have worked in the original implementation. Any idea why that might have been happening?
I.e:
launch {
    val result = getSomething()
    result.doSomethingSuspending()
    // code was running here fine
}
l

louiscad

02/05/2021, 5:16 PM
Unrelated change or repeated execution, or who knows, we don't have your codebase at hand 😄