diff --git a/mobile/lib/services/sync.service.dart b/mobile/lib/services/sync.service.dart index 658bffc44f..e7a192e783 100644 --- a/mobile/lib/services/sync.service.dart +++ b/mobile/lib/services/sync.service.dart @@ -138,7 +138,6 @@ class SyncService { Future _syncUsersFromServer(List users) async { users.sortBy((u) => u.id); final dbUsers = await _userRepository.getAll(sortBy: UserSort.id); - assert(dbUsers.isSortedBy((u) => u.id), "dbUsers not sorted!"); final List toDelete = []; final List toUpsert = []; final changes = diffSortedListsSync( @@ -322,8 +321,6 @@ class SyncService { ownerId: isShared ? null : me.isarId, sortBy: AlbumSort.remoteId, ); - assert(dbAlbums.isSortedBy((e) => e.remoteId!), "dbAlbums not sorted!"); - final List toDelete = []; final List existing = []; @@ -512,7 +509,6 @@ class SyncService { await _albumRepository.getAll(remote: false, sortBy: AlbumSort.localId); final List deleteCandidates = []; final List existing = []; - assert(inDb.isSorted((a, b) => a.localId!.compareTo(b.localId!)), "sort!"); final bool anyChanges = await diffSortedLists( onDevice, inDb, diff --git a/mobile/lib/utils/diff.dart b/mobile/lib/utils/diff.dart index 18e3843819..a36902d8c7 100644 --- a/mobile/lib/utils/diff.dart +++ b/mobile/lib/utils/diff.dart @@ -1,16 +1,20 @@ import 'dart:async'; +import 'package:collection/collection.dart'; + /// Efficiently compares two sorted lists in O(n), calling the given callback /// for each item. /// Return `true` if there are any differences found, else `false` -Future diffSortedLists( - List la, - List lb, { - required int Function(A a, B b) compare, - required FutureOr Function(A a, B b) both, - required FutureOr Function(A a) onlyFirst, - required FutureOr Function(B b) onlySecond, +Future diffSortedLists( + List la, + List lb, { + required int Function(T a, T b) compare, + required FutureOr Function(T a, T b) both, + required FutureOr Function(T a) onlyFirst, + required FutureOr Function(T b) onlySecond, }) async { + assert(la.isSorted(compare), "first argument must be sorted"); + assert(lb.isSorted(compare), "second argument must be sorted"); bool diff = false; int i = 0, j = 0; for (; i < la.length && j < lb.length;) { @@ -38,14 +42,16 @@ Future diffSortedLists( /// Efficiently compares two sorted lists in O(n), calling the given callback /// for each item. /// Return `true` if there are any differences found, else `false` -bool diffSortedListsSync( - List la, - List lb, { - required int Function(A a, B b) compare, - required bool Function(A a, B b) both, - required void Function(A a) onlyFirst, - required void Function(B b) onlySecond, +bool diffSortedListsSync( + List la, + List lb, { + required int Function(T a, T b) compare, + required bool Function(T a, T b) both, + required void Function(T a) onlyFirst, + required void Function(T b) onlySecond, }) { + assert(la.isSorted(compare), "first argument must be sorted"); + assert(lb.isSorted(compare), "second argument must be sorted"); bool diff = false; int i = 0, j = 0; for (; i < la.length && j < lb.length;) {