Fix regression about handling of Core Data duplicates (#841)

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"
This commit is contained in:
Davide 2024-11-10 18:13:59 +01:00 committed by GitHub
parent 21340e9f56
commit 44468b5d1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 4 additions and 2 deletions

View File

@ -205,7 +205,7 @@ private extension CoreDataRepository {
@discardableResult @discardableResult
func unsafeSendResults(from controller: NSFetchedResultsController<CD>) -> [T] { func unsafeSendResults(from controller: NSFetchedResultsController<CD>) -> [T] {
guard let cdEntities = controller.fetchedObjects else { guard var cdEntities = controller.fetchedObjects else {
return [] return []
} }
@ -226,6 +226,9 @@ private extension CoreDataRepository {
} }
do { do {
cdEntities.removeAll {
entitiesToDelete.contains($0)
}
let entities = try cdEntities.compactMap { let entities = try cdEntities.compactMap {
do { do {
return try fromMapper($0) return try fromMapper($0)
@ -243,7 +246,6 @@ private extension CoreDataRepository {
return nil return nil
} }
} }
if !entitiesToDelete.isEmpty { if !entitiesToDelete.isEmpty {
do { do {
entitiesToDelete.forEach(context.delete) entitiesToDelete.forEach(context.delete)