From a642b27d2b1cd1ddfae520f83552927ad4fba412 Mon Sep 17 00:00:00 2001 From: Davide Date: Mon, 9 Dec 2024 09:13:16 +0100 Subject: [PATCH] Deduplicate preferences by last update (#991) Fixes #990 --- .../Domain/CDModulePreferencesV3.swift | 1 + .../Domain/CDProviderPreferencesV3.swift | 1 + .../Preferences.xcdatamodel/contents | 2 ++ .../CDModulePreferencesRepositoryV3.swift | 15 ++++++++++++++- .../CDProviderPreferencesRepositoryV3.swift | 15 ++++++++++++++- 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Library/Sources/AppDataPreferences/Domain/CDModulePreferencesV3.swift b/Library/Sources/AppDataPreferences/Domain/CDModulePreferencesV3.swift index 3b5de56b..833bfeb2 100644 --- a/Library/Sources/AppDataPreferences/Domain/CDModulePreferencesV3.swift +++ b/Library/Sources/AppDataPreferences/Domain/CDModulePreferencesV3.swift @@ -33,5 +33,6 @@ final class CDModulePreferencesV3: NSManagedObject { } @NSManaged var uuid: UUID? + @NSManaged var lastUpdate: Date? @NSManaged var excludedEndpoints: Set? } diff --git a/Library/Sources/AppDataPreferences/Domain/CDProviderPreferencesV3.swift b/Library/Sources/AppDataPreferences/Domain/CDProviderPreferencesV3.swift index c9d44b7b..15ee8171 100644 --- a/Library/Sources/AppDataPreferences/Domain/CDProviderPreferencesV3.swift +++ b/Library/Sources/AppDataPreferences/Domain/CDProviderPreferencesV3.swift @@ -33,6 +33,7 @@ final class CDProviderPreferencesV3: NSManagedObject { } @NSManaged var providerId: String? + @NSManaged var lastUpdate: Date? @NSManaged var favoriteServerIds: Data? @NSManaged var excludedEndpoints: Set? } diff --git a/Library/Sources/AppDataPreferences/Preferences.xcdatamodeld/Preferences.xcdatamodel/contents b/Library/Sources/AppDataPreferences/Preferences.xcdatamodeld/Preferences.xcdatamodel/contents index f482947b..827465fd 100644 --- a/Library/Sources/AppDataPreferences/Preferences.xcdatamodeld/Preferences.xcdatamodel/contents +++ b/Library/Sources/AppDataPreferences/Preferences.xcdatamodeld/Preferences.xcdatamodel/contents @@ -6,11 +6,13 @@ + + diff --git a/Library/Sources/AppDataPreferences/Strategy/CDModulePreferencesRepositoryV3.swift b/Library/Sources/AppDataPreferences/Strategy/CDModulePreferencesRepositoryV3.swift index 7c28c4c1..6a9deea2 100644 --- a/Library/Sources/AppDataPreferences/Strategy/CDModulePreferencesRepositoryV3.swift +++ b/Library/Sources/AppDataPreferences/Strategy/CDModulePreferencesRepositoryV3.swift @@ -46,9 +46,22 @@ private final class CDModulePreferencesRepositoryV3: ModulePreferencesRepository entity = try context.performAndWait { let request = CDModulePreferencesV3.fetchRequest() request.predicate = NSPredicate(format: "uuid == %@", moduleId.uuidString) + request.sortDescriptors = [.init(key: "lastUpdate", ascending: false)] do { - let entity = try request.execute().first ?? CDModulePreferencesV3(context: context) + let entities = try request.execute() + + // dedup by lastUpdate + entities.enumerated().forEach { + guard $0.offset > 0 else { + return + } + $0.element.excludedEndpoints?.forEach(context.delete(_:)) + context.delete($0.element) + } + + let entity = entities.first ?? CDModulePreferencesV3(context: context) entity.uuid = moduleId + entity.lastUpdate = Date() return entity } catch { pp_log(.app, .error, "Unable to load preferences for module \(moduleId): \(error)") diff --git a/Library/Sources/AppDataPreferences/Strategy/CDProviderPreferencesRepositoryV3.swift b/Library/Sources/AppDataPreferences/Strategy/CDProviderPreferencesRepositoryV3.swift index 65231bc6..cdd5d32d 100644 --- a/Library/Sources/AppDataPreferences/Strategy/CDProviderPreferencesRepositoryV3.swift +++ b/Library/Sources/AppDataPreferences/Strategy/CDProviderPreferencesRepositoryV3.swift @@ -46,9 +46,22 @@ private final class CDProviderPreferencesRepositoryV3: ProviderPreferencesReposi entity = try context.performAndWait { let request = CDProviderPreferencesV3.fetchRequest() request.predicate = NSPredicate(format: "providerId == %@", providerId.rawValue) + request.sortDescriptors = [.init(key: "lastUpdate", ascending: false)] do { - let entity = try request.execute().first ?? CDProviderPreferencesV3(context: context) + let entities = try request.execute() + + // dedup by lastUpdate + entities.enumerated().forEach { + guard $0.offset > 0 else { + return + } + $0.element.excludedEndpoints?.forEach(context.delete(_:)) + context.delete($0.element) + } + + let entity = entities.first ?? CDProviderPreferencesV3(context: context) entity.providerId = providerId.rawValue + entity.lastUpdate = Date() return entity } catch { pp_log(.app, .error, "Unable to load preferences for provider \(providerId): \(error)")