Class AsyncDataProvider<T>
- All Implemented Interfaces:
ConfigurableFilterDataProvider<T,
,SerializablePredicate<T>, SerializablePredicate<T>> DataProvider<T,
,SerializablePredicate<T>> InMemoryDataProvider<T>
,Serializable
ListDataProvider
whose contents are gathered in an asynchronous query.
Instances are just a ListDataProvider
with support for asynchronous (re)loading of the underlying data
in a background thread using an AsyncTaskManager
. As a result, the UI never "locks up" while a backend
data query executes.
The associated AsyncTaskManager
provides status change notifications when a load operation starts,
completes, fails, or is canceled. These updates can be used to drive GUI loading spinners, etc.
Load operations are initiated via load()
, and may be canceled in progress via cancel()
.
Initiating a new load operation will cancel and replace any previous load operation still in progress.
All operations are atomic and race free. See AsyncTaskManager
for details.
Queries
The DataProvider
's fetch()
and size()
methods take a Query
parameter,
which includes basic filtering, sorting, and offset/limit information for the query. However, because these methods
are synchronous, they not compatible with this class' aynchronous loading model. That's why this class extends
ListDataProvider
, which applies the Query
to a list of objects already loaded into memory by other means.
The purpose of this class is therefore simply to refresh that list, aynchronously, when requested.
However, there may be scenarios where the aynchronous load operation can benefit from knowing the Query
currently
being used, i.e., most recently passed to fetch()
or size()
. For such scenarios, subclasses
could override these methods and trigger a new load operation if the Query
has changed, etc.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Callback interface for asynchronous loading of data for aAsyncDataProvider
. -
Constructor Summary
ConstructorDescriptionDefault constructor.AsyncDataProvider
(AsyncTaskManager<Stream<? extends T>> taskManager) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionlong
cancel()
Cancel any outstanding asynchronous load of this instance.AsyncTaskManager<Stream<? extends T>>
Get theAsyncTaskManager
that manages background load operations.long
load
(AsyncDataProvider.Loader<? extends T> loader) Trigger a new asynchronous load of this instance.protected void
updateFromLoad
(long id, Stream<? extends T> stream) Update this data provider's internal list of items using the new data from a successful load operation and fire a refresh notification.Methods inherited from class com.vaadin.flow.data.provider.ListDataProvider
fetch, getFilter, getItems, getSortComparator, setFilter, setSortComparator, size
Methods inherited from class com.vaadin.flow.data.provider.AbstractDataProvider
addDataProviderListener, addListener, fireEvent, refreshAll, refreshItem, refreshItem
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.vaadin.flow.data.provider.DataProvider
addDataProviderListener, getId, refreshAll, refreshItem, refreshItem, withConfigurableFilter, withConfigurableFilter, withConvertedFilter
Methods inherited from interface com.vaadin.flow.data.provider.InMemoryDataProvider
addFilter, addFilter, addFilterByValue, addSortComparator, addSortOrder, clearFilters, filteringBy, filteringBy, filteringByEquals, filteringByPrefix, filteringByPrefix, filteringBySubstring, filteringBySubstring, isInMemory, setFilter, setFilterByValue, setSortOrder
-
Constructor Details
-
AsyncDataProvider
public AsyncDataProvider()Default constructor.Caller still must configure an async executor via
getAsyncTaskManager
().
setAsyncExecutor()
.- Throws:
IllegalStateException
- if there is noVaadinSession
associated with the current thread
-
AsyncDataProvider
Constructor.- Parameters:
taskManager
- loading task manager- Throws:
IllegalStateException
- if there is noVaadinSession
associated with the current thread
-
-
Method Details
-
getAsyncTaskManager
Get theAsyncTaskManager
that manages background load operations.- Returns:
- this instance's
AsyncTaskManager
, never null
-
load
Trigger a new asynchronous load of this instance.If/when the given load task completes successfully, its results will completely replace the contents of this data provider.
See
Loader
for important requirements for the behavior ofloader
.- Parameters:
loader
- performs the load operation- Returns:
- unique ID for this load attempt
- Throws:
IllegalStateException
- if the current thread is not associated with the task manager's sessionIllegalStateException
- if this instance'sAsyncTaskManager
has no executor configuredIllegalArgumentException
- ifloader
is null- See Also:
-
cancel
public long cancel()Cancel any outstanding asynchronous load of this instance.- Returns:
- the unique ID of the canceled task, if any, or zero if there is no task outstanding
- Throws:
IllegalStateException
- if the current thread is not associated with the task manager's session- See Also:
-
updateFromLoad
Update this data provider's internal list of items using the new data from a successful load operation and fire a refresh notification.This is invoked with the task manager's session locked.
- Parameters:
id
- task IDstream
- load results- Throws:
IllegalStateException
- if the current thread is not associated with the task manager's sessionIllegalArgumentException
- ifstream
is null
-