From 8f7e06bebd1cbb9e607d8a6ce2ec478741c986af Mon Sep 17 00:00:00 2001 From: shenlong-tanwen <139912620+shalong-tanwen@users.noreply.github.com> Date: Sat, 10 Feb 2024 22:26:09 +0530 Subject: [PATCH] fix: use mutex for sync local albums --- .../modules/album/services/local_album.service.dart | 5 ++++- mobile/lib/shared/services/sync.service.dart | 10 +++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/mobile/lib/modules/album/services/local_album.service.dart b/mobile/lib/modules/album/services/local_album.service.dart index 213ec335ab..e45cdf87a8 100644 --- a/mobile/lib/modules/album/services/local_album.service.dart +++ b/mobile/lib/modules/album/services/local_album.service.dart @@ -26,9 +26,12 @@ class LocalAlbumService { LocalAlbumService(this._db, this._hashService, this._syncService); + Future 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 refreshDeviceAlbums() async { + Future _refreshDeviceAlbums() async { if (!_localCompleter.isCompleted) { // guard against concurrent calls _log.info("refreshDeviceAlbums is already in progress"); diff --git a/mobile/lib/shared/services/sync.service.dart b/mobile/lib/shared/services/sync.service.dart index 19608b3af3..8d4547340e 100644 --- a/mobile/lib/shared/services/sync.service.dart +++ b/mobile/lib/shared/services/sync.service.dart @@ -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 syncUsersFromServer(List 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?> 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 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 sharedAssetsToRemove( @@ -78,7 +78,7 @@ class SyncService { /// Syncs a new asset to the db. Returns `true` if successful Future syncNewAssetToDb(Asset newAsset) => - _lock.run(() => _syncNewAssetToDb(newAsset)); + lock.run(() => _syncNewAssetToDb(newAsset)); // private methods: