diff --git a/mobile/lib/shared/services/sync.service.dart b/mobile/lib/shared/services/sync.service.dart index fb9888258d..f195f5dbb6 100644 --- a/mobile/lib/shared/services/sync.service.dart +++ b/mobile/lib/shared/services/sync.service.dart @@ -34,36 +34,8 @@ class SyncService { /// Syncs users from the server to the local database /// Returns `true`if there were any changes - Future syncUsersFromServer(List users) async { - users.sortBy((u) => u.id); - final dbUsers = await _db.users.where().sortById().findAll(); - assert(dbUsers.isSortedBy((u) => u.id), "dbUsers not sorted!"); - final List toDelete = []; - final List toUpsert = []; - final changes = diffSortedListsSync( - users, - dbUsers, - compare: (User a, User b) => a.id.compareTo(b.id), - both: (User a, User b) { - if (!a.updatedAt.isAtSameMomentAs(b.updatedAt) || - a.isPartnerSharedBy != b.isPartnerSharedBy || - a.isPartnerSharedWith != b.isPartnerSharedWith) { - toUpsert.add(a); - return true; - } - return false; - }, - onlyFirst: (User a) => toUpsert.add(a), - onlySecond: (User b) => toDelete.add(b.isarId), - ); - if (changes) { - await _db.writeTxn(() async { - await _db.users.deleteAll(toDelete); - await _db.users.putAll(toUpsert); - }); - } - return changes; - } + Future syncUsersFromServer(List users) => + _lock.run(() => _syncUsersFromServer(users)); /// Syncs remote assets owned by the logged-in user to the DB /// Returns `true` if there were any changes @@ -120,6 +92,39 @@ class SyncService { // private methods: + /// Syncs users from the server to the local database + /// Returns `true`if there were any changes + Future _syncUsersFromServer(List users) async { + users.sortBy((u) => u.id); + final dbUsers = await _db.users.where().sortById().findAll(); + assert(dbUsers.isSortedBy((u) => u.id), "dbUsers not sorted!"); + final List toDelete = []; + final List toUpsert = []; + final changes = diffSortedListsSync( + users, + dbUsers, + compare: (User a, User b) => a.id.compareTo(b.id), + both: (User a, User b) { + if (!a.updatedAt.isAtSameMomentAs(b.updatedAt) || + a.isPartnerSharedBy != b.isPartnerSharedBy || + a.isPartnerSharedWith != b.isPartnerSharedWith) { + toUpsert.add(a); + return true; + } + return false; + }, + onlyFirst: (User a) => toUpsert.add(a), + onlySecond: (User b) => toDelete.add(b.isarId), + ); + if (changes) { + await _db.writeTxn(() async { + await _db.users.deleteAll(toDelete); + await _db.users.putAll(toUpsert); + }); + } + return changes; + } + /// Syncs a new asset to the db. Returns `true` if successful Future _syncNewAssetToDb(Asset a) async { final Asset? inDb =