Skip to content

Async Task Execution

Edvin Syse edited this page Apr 23, 2016 · 8 revisions

WikiDocumentationAsync Task Execution

Async Task Execution

A responsive UI needs to make sure that no long running tasks are performed on the UI thread. In a Tornado FX application, long running tasks are typically defined in Controllers. To ensure work is performed in the correct thread, you can use the runAsync function:

runAsync {
    customerController.listCustomers()
} ui {
    customerTable.items = it
}

The runAsync function simply wraps your code in a JavaFX Task and executes it. ui is an extension function defined on Task that will make sure the supplied code will run on the UI thread once the task completes. The parameter supplied to ui is the Task result.

If an error occurs in the async operation, the default Error Handler is called.

You can ofcourse just catch and handle any error directly in the async operation, or install a different error handler by calling the Task#setOnFailed function. Infact, you can augment your Task in all the usual ways the Task interface allows for.

Async Items for data driven components

The pattern described above can be further optimized for data driven components like TableView, ListView and ComboBox by utilizing the asyncItems function. Think of it as a setter that retrieves the data on a background thread and applies it to the items property on the ui thread automatically:

tableView.asyncItems { controller.listItems() }

The controller function is executed in the background and applied to the TableView's items property on the ui thread.

Next: Extension Functions

Clone this wiki locally