From 5cafd9794d2a6d1dc29e5e8f53a1ea310f12d891 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sat, 16 Apr 2022 11:21:33 +0200 Subject: [PATCH] Review a bit about duplicates handling - Read profile(withId:) without fetcher - Refine redundant log about duplicated profiles --- .../Managers/ProfileManager.swift | 7 ++-- .../ProfileManagerStrategy+CoreData.swift | 4 +-- .../Repositories/ProfileRepository.swift | 34 ++++++++----------- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift index 8e0537cc..323e214b 100644 --- a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift +++ b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManager.swift @@ -335,8 +335,11 @@ extension ProfileManager { allNames.remove(at: i) } let duplicates = Set(allNames) - - pp_log.debug("Duplicate profile names: \(duplicates)") + guard !duplicates.isEmpty else { + pp_log.debug("No duplicated profiles") + return + } + pp_log.debug("Duplicated profile names: \(duplicates)") var renamedProfiles: [Profile] = [] duplicates.forEach { name in diff --git a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManagerStrategy+CoreData.swift b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManagerStrategy+CoreData.swift index 91c804ac..9862ee7e 100644 --- a/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManagerStrategy+CoreData.swift +++ b/PassepartoutCore/Sources/PassepartoutProfiles/Managers/ProfileManagerStrategy+CoreData.swift @@ -35,7 +35,7 @@ extension ProfileManager { public init(persistence: Persistence) { profileRepository = ProfileRepository(persistence.context) - fetchedHeaders = profileRepository.headers() + fetchedHeaders = profileRepository.fetchedHeaders() } public var allHeaders: [UUID: Profile.Header] { @@ -43,7 +43,7 @@ extension ProfileManager { } public func profile(withId id: UUID) -> Profile? { - profileRepository.profile(withId: id).value + profileRepository.profile(withId: id) } public func saveProfiles(_ profiles: [Profile]) { diff --git a/PassepartoutCore/Sources/PassepartoutProfiles/Repositories/ProfileRepository.swift b/PassepartoutCore/Sources/PassepartoutProfiles/Repositories/ProfileRepository.swift index 8eced4be..ef5988b4 100644 --- a/PassepartoutCore/Sources/PassepartoutProfiles/Repositories/ProfileRepository.swift +++ b/PassepartoutCore/Sources/PassepartoutProfiles/Repositories/ProfileRepository.swift @@ -34,7 +34,7 @@ class ProfileRepository: Repository { self.context = context } - func headers() -> FetchedValueHolder<[UUID: Profile.Header]> { + func fetchedHeaders() -> FetchedValueHolder<[UUID: Profile.Header]> { let request: NSFetchRequest = CDProfile.fetchRequest() request.sortDescriptors = [ .init(keyPath: \CDProfile.uuid, ascending: true), @@ -62,9 +62,9 @@ class ProfileRepository: Repository { initial: [:] ) } - - func profile(withId id: UUID) -> FetchedValueHolder { - let request: NSFetchRequest = CDProfile.fetchRequest() + + func profile(withId id: UUID) -> Profile? { + let request = CDProfile.fetchRequest() request.sortDescriptors = [ .init(keyPath: \CDProfile.lastUpdate, ascending: false) ] @@ -72,22 +72,16 @@ class ProfileRepository: Repository { format: "uuid == %@", id.uuidString ) - return .init( - context: context, - request: request, - mapping: { - guard let dto = $0.first as? CDProfile else { - return nil - } - do { - return try ProfileMapper.toModel(dto) - } catch { - pp_log.error("Unable to map CDProfile: \(error)") - return nil - } - }, - initial: nil - ) + do { + let results = try context.fetch(request) + guard let recent = results.first else { + return nil + } + return try ProfileMapper.toModel(recent) + } catch { + pp_log.error("Unable to fetch profile: \(error)") + return nil + } } func saveProfiles(_ profiles: [Profile]) throws {