dataProvider.refreshAll() blocks UI

Hi

I am trying to update my grid whenever a new alert is inserted into my database. I have verified that the dataprovider infact has updated with a dataProvider.refreshAll() on a button click.

I’m now struggeling with trying to update this with a ChangeStream event, but the UI is halting for some reason. Here is my code:

  alertService.onChange()
      .subscribe(changeEvent -> {
          System.out.println(changeEvent);
          getUI().ifPresent(ui -> {
              ui.access(() -> {
                  dataProvider.refreshAll();
              });
          });
      });

If I only have the printout the changeEvent is indeed the changed document, I also have verified that the UI is accesed but the refreshAll() is completely blocking the whole ui thread after one fire of the change event and I have to restart app.

Any suggestions would be appriciated!

Found a workaround for any future reference. But still wondering why my first approach did’t work.

ExecutorService uiExecutor = Executors.newSingleThreadExecutor();
        alertService.onChange()
                .subscribe(changeEvent -> {
                    uiExecutor.submit(() -> {
                        getUI().ifPresent(ui -> {
                            ui.access(() -> {
                                dataProvider.refreshAll();
                            });
                        });
                    });
                });

We have some excellent new docs on background jobs and push coming out in the next days. Pinging @Petter so he can provide the link when it’s deployed

Here is the new documentation on server push: Server Push | Presentation Layer | Building Apps | Vaadin Docs

Here is the part about background jobs: Background Jobs | Application Layer | Building Apps | Vaadin Docs

2 Likes