How to refresh data in a Grid?

I have a Grid with an AbstractBackEndDataProvider. The provider reads data from a database and return new instances for every call. I have added a ValueProvider so that an instance can be mapped by database id to an existing item in the DataProvider.

When I call dataProvider.refreshAll() there will be a call to the fetchFromBackEnd() metod that, in our case, will fetch data from the database. The data is “matched” with the keyMapper in the DataProvider and therefore the new data from the database will be ignored.

The documentation say that we should use refreshItem(obj) to prevent “stale” items.

https://vaadin.com/docs/v10/flow/binding-data/tutorial-flow-data-provider.html#lazy-refresh
“For example Spring Data gives you new instances with every request, and making changes to the repository will make old instances of the same object “stale”. In these cases you should inform any interested component by calling dataProvider.refreshItem(newInstance)”

But what if another application is updating the database and I want to refresh the grid in my application. That wont work with refreshAll() and using refreshItem(obj) feels not like the right solution. See the attached image.

Is there a way to force a complete refresh of the DataProvider?
42802.jpg

I tried that before but got “Internal error” in the “client notification box”. I didn’t looked at the stack trace… It said “Null Pointer Exception”. I see that my ValueProvider is called with a null value and I didn’t handled that. I think the null value comes from the DataCommunicator.passivatedByUpdate but I’m not sure.

So, your solution works. Thanks!

There is a way. Before resetting the DataProvider, you can reset the KeyMapper itself, by calling:

grid.getDataCommunicator().getKeyMapper().removeAll();

(The KeyMapper is actually managed by the DataCommunicator, not the DataProvider - but that’s an implementation detail)

That will remove all keys from the memory. Remember to refresh the DataProvider after you clear the KeyMapper, so the new items are fetched accordingly.