https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

loloof64

03/21/2023, 9:11 PM
Hi ! I'm having an issue with coroutine in a Jetpack Compose for Desktop project. More in the thread 🧵 .
1
In my
@Composable
, I've createad a function
startClockIfAppropriated
, but it seems that the coroutine I'm trying to launch inside it never start.
All I get is a message "Ready to start clock coroutine !"
Any help appreciated 🙂
This is the complete source file. Apologizing for my very ugly style.
s

streetsofboston

03/21/2023, 9:36 PM
When are all these inner-functions called (especially the one you're asking about
startClockIfAppropriated
)?
l

loloof64

03/21/2023, 9:49 PM
In this
startNewGame()
function, I'm calling
startClockIfAppropriated()
. And I'm calling this function
startNewGame()
when the user clicks on a button. What happens here is that I'm passing a callback function to the `Screen.EditPosition`page of the function
navigation.push()
so that it will be executed when we're back in the game screen if the user validated the new (chess) position they edited in this
EditPosition
screen.
s

streetsofboston

03/21/2023, 9:55 PM
I'm a bit worried about the inner functions, where you capture implicitly the stack-frame of a
@Compose
function. I can imagine that the
rememberCoroutineScope
returns a CoroutineScope that may be cancelled or in a state that it can't launch coroutines. Try moving all your functions outside of your (top)
@Compose
function, make them global, and instead add parameters for those variable that you implicitly captured.
l

loloof64

03/21/2023, 10:57 PM
Thank you. I'll try to refactor the
@Compose
function
I don't know how to proceed, because of the number of state variables in the `@Composable`function. And I'm sure I can't use
ViewModel
because it belongs to Android.
s

streetsofboston

03/21/2023, 11:16 PM
You can put state in StateHolders instead, ViewModels are not required.
l

loloof64

03/21/2023, 11:17 PM
Thank you very much. I'm gonna have a look at those StateHolders 🙂
s

streetsofboston

03/21/2023, 11:20 PM

https://youtu.be/pCX9wvu-Bq0

8m:55s in this video
l

loloof64

03/21/2023, 11:38 PM
Thank you very much 🙂 I'm gonna learn from it 🙂
s

streetsofboston

03/21/2023, 11:43 PM
And here's a Bootcamp, self guided https://developer.android.com/compose-camp
l

loloof64

03/22/2023, 1:13 PM
I've removed all states from the GamePage
@Composable
, using two state holders instead. GamePageLogicState and GamePageClockState .
The good news is that the application works as before. The bad news is that I'm still having issue with the clock coroutine, which seems to have been cancelled before the time I need to start the clock. (I've just printed the context of the coroutine's job and I noticed that)
As the only cancellation I'm doing is happening once the clock is launched, I bet I've got an exception in the coroutine context. But neither adding an exception handler, not using try/catch helped me see any kind of error. So I'm stuck.
I found the issue, and learned something new on the same occasion. The coroutine was created in the GamePage composable, which I left when letting user edit its custom chess position, and so it was cancelled, as the GamePage was not in the scope anymore.
So I needed a way to hoist the coroutine scope.
5 Views