UI is not updating

Hi, I have this component which includes a Grid.
`
public class QueryBlock extends VerticalLayout {
public QueryBlock(Query query, QueryManager queryManager) {
this.query = query;
this.queryManager = queryManager;
this.queryName = query.getQueryName();
this.querySql = query.getQuerySql();
this.resultsGrid = new Grid<>();
add(
queryNameH3,
querySqlPara,
runButton
);

    List<Map<String, Object>> results = query.getResults();
    ListDataProvider<Map<String, Object>> resultsProvider = new ListDataProvider<>(results);
    if (!results.isEmpty())
        hasResults = true;

    if (hasResults) {
        resultsGrid.setItems(resultsProvider);
        results.get(0).keySet().forEach(key -> resultsGrid.addColumn(map -> map.get(key)).setHeader(key).setAutoWidth(true));
        add(
                resultsGrid
        );
    } else {
        add(
                noResultsPara
        );
    }
}

// Asynchronous updating of grids
@Override
protected void onAttach(AttachEvent attachEvent) {
    feederThread = new Thread(() -> {
        while (true) {
            try {
                getUI().ifPresent(u -> u.access(() -> {
                    resultsGrid.getDataProvider().refreshAll();

// resultsGrid.setItems(query.getResults());

                }));
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    feederThread.start();
}

// detach method

}`

Above code is not updating the list in the UI. But if I refresh the page or manually click button that replaces the list in the grid, new changes will be visible. Can someone help me to find the error here?

full code is available here: https://gist.github.com/nimanthadilz/283940c0688aac16788176467f0a920c

Make sure to read https://vaadin.com/docs/latest/advanced/server-push

Thank you for the reply. I read the documentation too. May be I am doing something pretty obvious. Did you notice anything wrong in my code? Please help.

I have spent a long time trying to fix this. But I couldn’t.

You can start by adding system outs or break points to debug your feeder thread and make sure to enable Push by using the annotation

@SpringBootApplication @EnableAutoConfiguration @EnableScheduling @Push public class MonitoringApplication extends SpringBootServletInitializer implements AppShellConfigurator

@Push is added to the main class.
I did system outs and checked. I added a system out in the while(true) loop and checked if the new data is available in the list returned by the query.getResults(). It is there in the list.

But the problem is resultsGrid.getDataProvider().refreshAll() is not updating the data. Not updated in the UI.

I quickly looked at your code, and it may be that I don’t now understand it perfectly, but couple of considerations:

  • Don’t use DataProvider at all. In the way you seem to be fetching data from your backend, you shouldn’t need that at all. It is just much easier to set the new list coming from your backend to the grid (with setItems). No refreshAlls or anything like that is needed.
  • Your feederThread seems somehow invalid. I think you there want to poll the backend for new data, but you are only doing that dataprovider magic that shouldn’t be needed at all. Maybe you should call your runQuery method there?

Hi, I tried without DataProvider and called the resultsgrid.setItems(query.getResults()) instead of call the runQuery() method as you suggested.

This works.

The reason why I am not calling runQuery method inside my QueryBlock is it does a database call. I don’t want to do a database call every 5secs. The data (the results list) in the backend is updated using a scheduled task in Spring. In the UI what I want is to just update the list as soon as the results list is updated without refreshing the page.

My concern now is what is the impact of using resultsGrid.setItems() vs. resultsGrid.getDataProvider().refreshAll() methods?
I think setItems replaces the whole list with a new list.

There is no difference. The whole list updated anyways.