Fix inverted profile lastUpdate logic

- Least recent was set last in the headers dictionary, thus
overwriting most recent (ascending = false)

- Mapping full profiles directly to array was generating
duplicates, use a dictionary to keep ID unicity
This commit is contained in:
Davide De Rosa 2022-07-18 17:29:49 +02:00
parent 0464cd0476
commit 1c64253a1f
1 changed files with 9 additions and 3 deletions

View File

@ -39,7 +39,7 @@ class ProfileRepository: Repository {
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),
.init(keyPath: \CDProfile.lastUpdate, ascending: false) .init(keyPath: \CDProfile.lastUpdate, ascending: true)
] ]
request.propertiesToFetch = [ request.propertiesToFetch = [
"uuid", "uuid",
@ -69,11 +69,17 @@ class ProfileRepository: Repository {
let request = CDProfile.fetchRequest() let request = CDProfile.fetchRequest()
request.sortDescriptors = [ request.sortDescriptors = [
.init(keyPath: \CDProfile.uuid, ascending: true), .init(keyPath: \CDProfile.uuid, ascending: true),
.init(keyPath: \CDProfile.lastUpdate, ascending: false) .init(keyPath: \CDProfile.lastUpdate, ascending: true)
] ]
do { do {
let results = try context.fetch(request) let results = try context.fetch(request)
return try results.compactMap(ProfileMapper.toModel) let map = try results.reduce(into: [UUID: Profile]()) {
guard let profile = try ProfileMapper.toModel($1) else {
return
}
$0[profile.id] = profile
}
return Array(map.values)
} catch { } catch {
pp_log.error("Unable to fetch profiles: \(error)") pp_log.error("Unable to fetch profiles: \(error)")
return [] return []