From 44468b5d1f5e0a70a72257655f3c71853917d207 Mon Sep 17 00:00:00 2001 From: Davide Date: Sun, 10 Nov 2024 18:13:59 +0100 Subject: [PATCH] Fix regression about handling of Core Data duplicates (#841) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Regression in #839 due to how NSFetchedResultsController was refactored. Duplicated entities were not excluded from mapping. Could "crash" the app with these easy steps: - Pick a profile - Unshare the profile on iOS - Unshare the profile on macOS - Re-share the profile on iCloud on both iOS and macOS - Save the profile simultaneously on iOS/macOS - Assertion failure due to duplicates in ProfileManager.reloadRemoteProfiles() → "Remote repository must not have duplicates" --- .../Sources/CommonUtils/Business/CoreDataRepository.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Passepartout/Library/Sources/CommonUtils/Business/CoreDataRepository.swift b/Passepartout/Library/Sources/CommonUtils/Business/CoreDataRepository.swift index 03e0d7a4..e9b507bf 100644 --- a/Passepartout/Library/Sources/CommonUtils/Business/CoreDataRepository.swift +++ b/Passepartout/Library/Sources/CommonUtils/Business/CoreDataRepository.swift @@ -205,7 +205,7 @@ private extension CoreDataRepository { @discardableResult func unsafeSendResults(from controller: NSFetchedResultsController) -> [T] { - guard let cdEntities = controller.fetchedObjects else { + guard var cdEntities = controller.fetchedObjects else { return [] } @@ -226,6 +226,9 @@ private extension CoreDataRepository { } do { + cdEntities.removeAll { + entitiesToDelete.contains($0) + } let entities = try cdEntities.compactMap { do { return try fromMapper($0) @@ -243,7 +246,6 @@ private extension CoreDataRepository { return nil } } - if !entitiesToDelete.isEmpty { do { entitiesToDelete.forEach(context.delete)