deleteDocs method
Deletes multiple documents both locally and from Firestore.
Performs optimistic deletes 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 deletedoNotifyListeners
- Whether to notify listeners of the changes
Returns a TurboResponse
indicating success or failure
Implementation
@protected
Future<TurboResponse> deleteDocs({
Transaction? transaction,
required List<String> ids,
bool doNotifyListeners = true,
}) async {
try {
log.debug('Deleting ${ids.length} docs');
deleteLocalDocs(
ids: ids,
doNotifyListeners: doNotifyListeners,
);
if (transaction != null) {
for (final id in ids) {
(await api.deleteDoc(
id: id,
transaction: transaction,
))
.throwWhenFail();
}
return TurboResponse.successAsBool();
} else {
final batch = api.writeBatch;
for (final id in ids) {
await api.deleteDocInBatch(
id: id,
writeBatch: batch,
);
}
final future = batch.commit();
await future;
return TurboResponse.successAsBool();
}
} catch (error, stackTrace) {
if (transaction != null) rethrow;
log.error(
'${error.runtimeType} caught while deleting docs',
error: error,
stackTrace: stackTrace,
);
return TurboResponse.fail(error: error);
}
}