updateDocs method
Future<TurboResponse<List<T> > >
updateDocs({
- Transaction? transaction,
- required List<
String> ids, - required UpdateDocDef<
T> doc, - bool doNotifyListeners = true,
Updates multiple documents both locally and in Firestore.
Performs optimistic updates by updating the local state first, then syncing with Firestore. Uses a batch operation for multiple documents unless a transaction is provided.
Parameters:
transaction
- Optional transaction for atomic operationsids
- The IDs of the documents to updatedoc
- The definition of how to update the documentsdoNotifyListeners
- Whether to notify listeners of the changes
Returns a TurboResponse
indicating success or failure
Implementation
@protected
Future<TurboResponse<List<T>>> updateDocs({
Transaction? transaction,
required List<String> ids,
required UpdateDocDef<T> doc,
bool doNotifyListeners = true,
}) async {
try {
log.debug('Updating ${ids.length} docs');
final pDocs = updateLocalDocs(
ids: ids,
doc: doc,
doNotifyListeners: doNotifyListeners,
);
if (transaction != null) {
for (final pDoc in pDocs) {
(await api.updateDoc(
id: pDoc.id,
transaction: transaction,
writeable: pDoc,
))
.throwWhenFail();
}
return TurboResponse.success(result: pDocs);
} else {
final batch = api.writeBatch;
for (final pDoc in pDocs) {
await api.updateDocInBatch(
id: pDoc.id,
writeBatch: batch,
writeable: pDoc,
);
}
final future = batch.commit();
await future;
return TurboResponse.success(result: pDocs);
}
} catch (error, stackTrace) {
if (transaction != null) rethrow;
log.error(
'${error.runtimeType} caught while updating docs',
error: error,
stackTrace: stackTrace,
);
return TurboResponse.fail(error: error);
}
}