Retry batch insert requests

This commit is contained in:
Vishnu Mohandas 2021-02-26 13:22:21 +05:30
parent 6fb07bab27
commit ffefa99504

View file

@ -25,6 +25,8 @@ class CollectionsService {
static final _collectionSyncTimeKeyPrefix = "collection_sync_time_";
static final _collectionsSyncTimeKey = "collections_sync_time";
static const int kMaximumWriteAttempts = 5;
final _logger = Logger("CollectionsService");
CollectionsDB _db;
@ -76,7 +78,7 @@ class CollectionsService {
? collection.updationTime
: maxUpdationTime;
}
await _db.insert(updatedCollections);
await _updateDB(updatedCollections);
_prefs.setInt(_collectionsSyncTimeKey, maxUpdationTime);
final collections = await _db.getAllCollections();
for (final collection in collections) {
@ -381,6 +383,18 @@ class CollectionsService {
return collection;
}
}
Future _updateDB(List<Collection> collections, {int attempt = 1}) async {
try {
await _db.insert(collections);
} catch (e) {
if (attempt < kMaximumWriteAttempts) {
return _updateDB(collections, attempt: attempt++);
} else {
throw e;
}
}
}
}
class AddFilesRequest {