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)
}
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

View File

@ -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]) {

View File

@ -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<NSFetchRequestResult> = CDProfile.fetchRequest()
request.sortDescriptors = [
.init(keyPath: \CDProfile.uuid, ascending: true),
@ -62,9 +62,9 @@ class ProfileRepository: Repository {
initial: [:]
)
}
func profile(withId id: UUID) -> FetchedValueHolder<Profile?> {
let request: NSFetchRequest<NSFetchRequestResult> = 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 {