How to get access UI thread from child thread in Vaadin 8


How to get access UI thread from child thread in Vaadin 8



I used Vaadin 7 before. Where I could access UI thread from my child thread by using following code:-


UI.getCurrent().access(() -> {
status.setVisible(true);
});



But currently, I am migrating from Vaddin 7 to Vaadin 8. In Vaadin 8 UI.getCurrent() returns null form the child thread. So how can we get access to UI thread from child thread?




1 Answer
1



The preferred pattern in Vaadin is to apply something MVP like (Model View Presenter).



When you create a View by extending a Layout has you can use getUI() method, which returns right UI instance when Layout & View is attached. This means that in your class implementing View you can also implement method that updates the status, say something like:


updateStatus(boolean visible) {
getUI().access(() -> {
status.setVisible(true);
});
}



If your application is prone to users closing browser eagerly, bad network conditions, etc. you may want to surround the access(..) in try catch and catch UIDetachedException, which may occur if browser connection is suddenly lost.



Your process in background thread can call this method safely, i.e. myView.updateStatus(true). Usually this is done via Presenter


presenter.getView().updateStatus(true);





Secondary option is to store the UI reference in Presenter's constructor (UI ui = UI.getCurrent()) , since that is also called in UI context and safe and then later use ui.access(..).
– Tatu Lund
Jul 2 at 18:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

JMeter fails on beanshell imports

Why in node-red my HTTP POST no receive payload from inject?

PHP contact form sending but not receiving emails