I dont understand this. The warning says suspendin...
# coroutines
l
I dont understand this. The warning says suspending fun has a CoroutineScope receiver, but the second class fails to find the "launch" method. Is this a bug or am i really misunderstanding this?
t
Not sure exactly what you're trying to do, but
Copy code
class Test{
 suspend fun test() = coroutineScope{
 launch{ do things}
}
}
might be what you're looking for
but that launch will stop returning from test() until it completes
l
Well, i'm trying to use coroutines with JavaFX, but that doesnt seem to be very usual and i didnt find much on it apart from the Dispatchers.JavaFX (or Main) One elegant solution i found was to make my components extend the CoroutineScope and clear it accordingly to javafx components lifecycle The classes were minimal reproducers But regardless of what i'm trying to do, i still dont quite understand the warning. Why cant i call launch from a suspending function?
Ok i tried your code and i makes a bit more sense now. Still some things i need to figure out tho, thanks!
i
Your outer class "test" implements coroutinescope, so launch in your function is
this.launch
c
You are already in a suspend function. There is no need to
launch
a coroutine. That's what the warning is telling you.
l
I dont think thats it @Chrimaeon
Copy code
suspend fun test() {
 launch{ do things}
 do other things
}
What if i wanted this? It would still show a warning
i
To get access to coroutine scope in suspending function you can do
Copy code
suspend fun test() {
  coroutineScope {
    // access scope here
  }
}
c
Sure, but then also you’ll need to wrap the
launch
in a “new”
coroutineScope{}
like already mentioned, to give the job it’s own inner scope
l
Yeah i think i was misunderstanding some basic concepts about context vs scope
Like why could i use withContext but not launch
t
withContext provides a coroutineScope in the block and doesn't require one to run
l
Implementing CoroutineScope is the problem. Should have a private property instead, and use
coroutineScope { }
inside suspend functions.