fix: use mutex for sync local albums

This commit is contained in:
shenlong-tanwen
2024-02-10 22:26:09 +05:30
parent eac150114f
commit 8f7e06bebd
2 changed files with 9 additions and 6 deletions
@@ -26,9 +26,12 @@ class LocalAlbumService {
LocalAlbumService(this._db, this._hashService, this._syncService);
Future<bool> refreshDeviceAlbums() async =>
SyncService.lock.run(_refreshDeviceAlbums);
/// Checks all selected device albums for changes of albums and their assets
/// Updates the local database and returns `true` if there were any changes
Future<bool> refreshDeviceAlbums() async {
Future<bool> _refreshDeviceAlbums() async {
if (!_localCompleter.isCompleted) {
// guard against concurrent calls
_log.info("refreshDeviceAlbums is already in progress");
+5 -5
View File
@@ -23,7 +23,7 @@ final syncServiceProvider = Provider(
class SyncService {
final Isar _db;
final AsyncMutex _lock = AsyncMutex();
static final AsyncMutex lock = AsyncMutex();
final Logger _log = Logger('SyncService');
SyncService(this._db);
@@ -33,7 +33,7 @@ class SyncService {
/// Syncs users from the server to the local database
/// Returns `true`if there were any changes
Future<bool> syncUsersFromServer(List<User> users) =>
_lock.run(() => _syncUsersFromServer(users));
lock.run(() => _syncUsersFromServer(users));
/// Syncs remote assets owned by the logged-in user to the DB
/// Returns `true` if there were any changes
@@ -45,7 +45,7 @@ class SyncService {
) getChangedAssets,
FutureOr<List<Asset>?> Function(User user) loadAssets,
) =>
_lock.run(
lock.run(
() async =>
await _syncRemoteAssetChanges(user, getChangedAssets) ??
await _syncRemoteAssetsFull(user, loadAssets),
@@ -58,7 +58,7 @@ class SyncService {
required bool isShared,
required FutureOr<AlbumResponseDto> Function(AlbumResponseDto) loadDetails,
}) =>
_lock.run(() => _syncRemoteAlbumsToDb(remote, isShared, loadDetails));
lock.run(() => _syncRemoteAlbumsToDb(remote, isShared, loadDetails));
/// returns all Asset IDs that are not contained in the existing list
List<int> sharedAssetsToRemove(
@@ -78,7 +78,7 @@ class SyncService {
/// Syncs a new asset to the db. Returns `true` if successful
Future<bool> syncNewAssetToDb(Asset newAsset) =>
_lock.run(() => _syncNewAssetToDb(newAsset));
lock.run(() => _syncNewAssetToDb(newAsset));
// private methods: