From e82dac3152152e5d3b5719742761606dbc8dc8a8 Mon Sep 17 00:00:00 2001 From: Davide Date: Mon, 18 Nov 2024 10:56:54 +0100 Subject: [PATCH] Clear previous remote import task (#888) Previous task was never nil-ed out on completion. --- .../Business/ProfileManager.swift | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Passepartout/Library/Sources/CommonLibrary/Business/ProfileManager.swift b/Passepartout/Library/Sources/CommonLibrary/Business/ProfileManager.swift index 8ea6f55a..ff95fd03 100644 --- a/Passepartout/Library/Sources/CommonLibrary/Business/ProfileManager.swift +++ b/Passepartout/Library/Sources/CommonLibrary/Business/ProfileManager.swift @@ -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) {