-
Notifications
You must be signed in to change notification settings - Fork 272
Async Task Execution
Wiki ▸ Documentation ▸ 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 background
function:
background {
customerController.listCustomers()
} ui {
customerTable.items = it
}
The background
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 background operation, the default Error Handler is called.
You can ofcourse just catch and handle any error directly in the background 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.
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