I have a strange issue where I do some work in a background thread and use a callback to update the UI. I have @Push on the root @Route component and the callback uses UI.access and finishes with a UI.push() for good measure but for some reason a Notification.show() turns up but e.g. an Label.setText doesn’t refresh.
How can the notification work but not the component update? Both are just changes to the DOM, right? Have I had too little coffee (or too much)?
Hmm. And now I get an “no UI instance available”. Which is strange since the update is being done within UI.access (UI instance stored away in view). However, if I refresh the browser, it works!
Yes, Notifications and component updates should be the same from the Push point of view. UI.push() shouldn’t be needed, either, unless you have explicitly set up manual push mode.
How are you getting the UI instance? Typically, it’s easiest to get it through a Component (or just a UI reference) you have passed on to the backing thread.
I pick up a UI.getCurrent() and store it in the view and do the UI.access through that. I’ve verified with UI.getUIId and eclipse debugger instance number that it’s the same one being used. There shouldn’t™ be any difference in passing along the actual instance all the way to the background thread vs e.g. accessing the same instance through a lambda?
The plot thickens. If I go to my view by typing in the URL, @Push enables push but if I end up on my view through navigation, it’s disabled.
@Route("test1")
@RouteScoped
public class ViewA extends Div {
public ViewA() {
Button nav = new Button("navigate", e -> UI.getCurrent().navigate("test2"));
add(nav);
}
}
@Route("test2")
@RouteScoped
@Push
public class ViewB extends Div implements BeforeEnterObserver {
@Override
public void beforeEnter(BeforeEnterEvent event) {
System.out.println(UI.getCurrent().getPushConfiguration().getPushMode());
}
}