fetch method
Future<List<T> >
fetch(
- DatumQuery query, {
- DataFetchStrategy strategy = DataFetchStrategy.localFirst,
- String? userId,
- bool persistRemoteResults = false,
Runs query using a DataFetchStrategy, removing the repetitive
"try local, then fall back to remote" boilerplate (#17).
- DataFetchStrategy.localOnly / DataFetchStrategy.remoteOnly: single source.
- DataFetchStrategy.localFirst: local first; if empty, fetch remote
(and, when
persistRemoteResultsis true, best-effort save them locally). - DataFetchStrategy.remoteFirst: remote first; on error, fall back to local.
Implementation
Future<List<T>> fetch(
DatumQuery query, {
DataFetchStrategy strategy = DataFetchStrategy.localFirst,
String? userId,
bool persistRemoteResults = false,
}) async {
_ensureInitialized();
switch (strategy) {
case DataFetchStrategy.localOnly:
return this.query(query, source: DataSource.local, userId: userId);
case DataFetchStrategy.remoteOnly:
return this.query(query, source: DataSource.remote, userId: userId);
case DataFetchStrategy.localFirst:
final local = await this.query(query, source: DataSource.local, userId: userId);
if (local.isNotEmpty) return local;
final remote = await this.query(query, source: DataSource.remote, userId: userId);
if (persistRemoteResults && remote.isNotEmpty) {
await _persistFetchedLocally(remote);
}
return remote;
case DataFetchStrategy.remoteFirst:
try {
final remote = await this.query(query, source: DataSource.remote, userId: userId);
if (persistRemoteResults && remote.isNotEmpty) {
await _persistFetchedLocally(remote);
}
return remote;
} catch (e) {
_logger.warn('remoteFirst fetch failed, falling back to local: $e');
return this.query(query, source: DataSource.local, userId: userId);
}
}
}