Review a bit about duplicates handling

- Read profile(withId:) without fetcher

- Refine redundant log about duplicated profiles
This commit is contained in:
Davide De Rosa 2022-04-16 11:21:33 +02:00
parent c0f105fedb
commit 5cafd9794d
3 changed files with 21 additions and 24 deletions

View File

@ -335,8 +335,11 @@ extension ProfileManager {
allNames.remove(at: i) allNames.remove(at: i)
} }
let duplicates = Set(allNames) let duplicates = Set(allNames)
guard !duplicates.isEmpty else {
pp_log.debug("Duplicate profile names: \(duplicates)") pp_log.debug("No duplicated profiles")
return
}
pp_log.debug("Duplicated profile names: \(duplicates)")
var renamedProfiles: [Profile] = [] var renamedProfiles: [Profile] = []
duplicates.forEach { name in duplicates.forEach { name in

View File

@ -35,7 +35,7 @@ extension ProfileManager {
public init(persistence: Persistence) { public init(persistence: Persistence) {
profileRepository = ProfileRepository(persistence.context) profileRepository = ProfileRepository(persistence.context)
fetchedHeaders = profileRepository.headers() fetchedHeaders = profileRepository.fetchedHeaders()
} }
public var allHeaders: [UUID: Profile.Header] { public var allHeaders: [UUID: Profile.Header] {
@ -43,7 +43,7 @@ extension ProfileManager {
} }
public func profile(withId id: UUID) -> Profile? { public func profile(withId id: UUID) -> Profile? {
profileRepository.profile(withId: id).value profileRepository.profile(withId: id)
} }
public func saveProfiles(_ profiles: [Profile]) { public func saveProfiles(_ profiles: [Profile]) {

View File

@ -34,7 +34,7 @@ class ProfileRepository: Repository {
self.context = context self.context = context
} }
func headers() -> FetchedValueHolder<[UUID: Profile.Header]> { func fetchedHeaders() -> FetchedValueHolder<[UUID: Profile.Header]> {
let request: NSFetchRequest<NSFetchRequestResult> = CDProfile.fetchRequest() let request: NSFetchRequest<NSFetchRequestResult> = CDProfile.fetchRequest()
request.sortDescriptors = [ request.sortDescriptors = [
.init(keyPath: \CDProfile.uuid, ascending: true), .init(keyPath: \CDProfile.uuid, ascending: true),
@ -62,9 +62,9 @@ class ProfileRepository: Repository {
initial: [:] initial: [:]
) )
} }
func profile(withId id: UUID) -> FetchedValueHolder<Profile?> { func profile(withId id: UUID) -> Profile? {
let request: NSFetchRequest<NSFetchRequestResult> = CDProfile.fetchRequest() let request = CDProfile.fetchRequest()
request.sortDescriptors = [ request.sortDescriptors = [
.init(keyPath: \CDProfile.lastUpdate, ascending: false) .init(keyPath: \CDProfile.lastUpdate, ascending: false)
] ]
@ -72,22 +72,16 @@ class ProfileRepository: Repository {
format: "uuid == %@", format: "uuid == %@",
id.uuidString id.uuidString
) )
return .init( do {
context: context, let results = try context.fetch(request)
request: request, guard let recent = results.first else {
mapping: { return nil
guard let dto = $0.first as? CDProfile else { }
return nil return try ProfileMapper.toModel(recent)
} } catch {
do { pp_log.error("Unable to fetch profile: \(error)")
return try ProfileMapper.toModel(dto) return nil
} catch { }
pp_log.error("Unable to map CDProfile: \(error)")
return nil
}
},
initial: nil
)
} }
func saveProfiles(_ profiles: [Profile]) throws { func saveProfiles(_ profiles: [Profile]) throws {