Clear previous remote import task (#888)

Previous task was never nil-ed out on completion.
This commit is contained in:
Davide 2024-11-18 10:56:54 +01:00 committed by GitHub
parent a3dad4e445
commit e82dac3152
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 9 deletions

View File

@ -380,7 +380,9 @@ private extension ProfileManager {
if waitingObservers.contains(.remote) {
waitingObservers.remove(.remote) // @Published
}
importRemoteProfiles(result)
Task { [weak self] in
await self?.importRemoteProfiles(result)
}
objectWillChange.send()
}
@ -404,11 +406,18 @@ private extension ProfileManager {
}
}
func importRemoteProfiles(_ profiles: [Profile]) {
func importRemoteProfiles(_ profiles: [Profile]) async {
guard !profiles.isEmpty else {
return
}
if let previousTask = remoteImportTask {
pp_log(.App.profiles, .info, "Cancel ongoing remote import...")
previousTask.cancel()
await previousTask.value
remoteImportTask = nil
}
pp_log(.App.profiles, .info, "Start importing remote profiles: \(profiles.map(\.id)))")
assert(profiles.count == Set(profiles.map(\.id)).count, "Remote repository must not have duplicates")
@ -426,18 +435,11 @@ private extension ProfileManager {
let remotelyDeletedIds = Set(allProfiles.keys).subtracting(Set(allRemoteProfiles.keys))
let mirrorsRemoteRepository = mirrorsRemoteRepository
let previousTask = remoteImportTask
remoteImportTask = Task.detached { [weak self] in
guard let self else {
return
}
if let previousTask {
pp_log(.App.profiles, .info, "Cancel ongoing remote import...")
previousTask.cancel()
await previousTask.value
}
var idsToRemove: [Profile.ID] = []
if !remotelyDeletedIds.isEmpty {
pp_log(.App.profiles, .info, "Will \(mirrorsRemoteRepository ? "delete" : "retain") local profiles not present in remote repository: \(remotelyDeletedIds)")
@ -480,6 +482,8 @@ private extension ProfileManager {
// yield a little bit
try? await Task.sleep(for: .milliseconds(100))
}
await remoteImportTask?.value
remoteImportTask = nil
}
func reloadFilteredProfiles(with search: String) {