tornadofx icon indicating copy to clipboard operation
tornadofx copied to clipboard

tornadofx can work with kotlin coroutine ?

Open toplinuxsir opened this issue 6 years ago • 8 comments

Tornadofx can work with kotlin coroutine ? any examples? Thanks!

toplinuxsir avatar Jan 08 '19 07:01 toplinuxsir

I know plenty of people are using coroutines with TornadoFX, though there is no special support for it. Not sure if it's even needed :) Take a look here for examples:

https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md#javafx

Better yet, ask in the Slack channel, I'm sure some of the guys can give you some example code!

edvin avatar Jan 08 '19 08:01 edvin

@edvin Thanks a lot , I got it

toplinuxsir avatar Jan 09 '19 00:01 toplinuxsir

do you success?The IDEA show me a error that Suspend function 'delay' should be called only from a coroutine or another suspend function when I use the method named delay in coroutines.

I use the library kotlinx-coroutines-javafx,version is 1.3.5

Following is my code

class TestView() : View("My View") {

    override val root = vbox {
        button {
            action {      
                GlobalScope.launch(Dispatchers.UI){
                    //idea show a error tip
                    delay(200L)
                }
            }
        }
    }
}

stars-one avatar May 08 '20 03:05 stars-one

@Stars-One This code works for me, but why do you use Dispatchers.UI you need Dispatchers.JavaFx. what version of kotlin do you have?

SchweinchenFuntik avatar May 08 '20 04:05 SchweinchenFuntik

also I would not recommend using GlobalScope. Work with coroutines can be hidden in the controller. You can also write auxiliary extension functions for calling coruntin

SchweinchenFuntik avatar May 08 '20 05:05 SchweinchenFuntik

yes,I know the Dispatchers.UI,It also show the same error tip.I just want to finish a simple example.

I use the kotlin verison 1.3.70,maybe the reason is the version of idea?my idea version is 2018.2 @SchweinchenFuntik

stars-one avatar May 08 '20 06:05 stars-one

most likely yes, new versions of the kotlin compiler didn’t seem to work with the old IDEA

SchweinchenFuntik avatar May 08 '20 06:05 SchweinchenFuntik

Try this. Use the JavaFX Dispatcher.

GlobalScope.launch(Dispatchers.IO) {
    try {
        val res = api.put<ApiSuccess<Entity>>("$api_url/entities") {
            body = entity
        }
        launch(Dispatchers.JavaFx) {
            fire(EntityUpdatedEvent(res.data))
        }
    } catch (e: ApiException) {
        ErrorMessage("..err.", e.apiError.error).show()
    }
}

doughoff avatar Mar 26 '21 02:03 doughoff