From 52232d63f0379d9f0b9caf4b91a2331f13fcedac Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 21 Sep 2024 11:55:05 -0500 Subject: [PATCH] Fix threading for Collaboration tasks --- .../CloudKit/CloudKitManager.swift | 58 +++++++++++-------- xcconfig/Zavala_target_common.xcconfig | 2 +- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/VinOutlineKit/Sources/VinOutlineKit/CloudKit/CloudKitManager.swift b/VinOutlineKit/Sources/VinOutlineKit/CloudKit/CloudKitManager.swift index 5bd9ab61..38a4796b 100644 --- a/VinOutlineKit/Sources/VinOutlineKit/CloudKit/CloudKitManager.swift +++ b/VinOutlineKit/Sources/VinOutlineKit/CloudKit/CloudKitManager.swift @@ -173,36 +173,40 @@ public class CloudKitManager { cloudKitSyncWillBegin() return await withCheckedContinuation { continuation in - let op = CKAcceptSharesOperation(shareMetadatas: [shareMetadata]) - op.qualityOfService = CloudKitOutlineZone.qualityOfService - - op.acceptSharesResultBlock = { [weak self] result in + Task.detached { [weak self] in guard let self else { return } - switch result { - case .success: - let zoneID = shareMetadata.share.recordID.zoneID - Task { - self.cloudKitSyncWillBegin() - do { - try await self.fetchChanges(userInitiated: true, zoneID: zoneID) - self.cloudKitSyncDidComplete() - continuation.resume() - } catch { - self.presentError(error) - self.cloudKitSyncDidComplete() + let op = CKAcceptSharesOperation(shareMetadatas: [shareMetadata]) + op.qualityOfService = CloudKitOutlineZone.qualityOfService + + op.acceptSharesResultBlock = { [weak self] result in + guard let self else { return } + + switch result { + case .success: + let zoneID = shareMetadata.share.recordID.zoneID + Task { + await self.cloudKitSyncWillBegin() + do { + try await self.fetchChanges(userInitiated: true, zoneID: zoneID) + await self.cloudKitSyncDidComplete() + continuation.resume() + } catch { + await self.presentError(error) + await self.cloudKitSyncDidComplete() + continuation.resume() + } + } + case .failure(let error): + Task { + await self.presentError(error) continuation.resume() } } - case .failure(let error): - Task { - self.presentError(error) - continuation.resume() - } } + + self.container.add(op) } - - container.add(op) } } @@ -396,7 +400,9 @@ private extension CloudKitManager { } try await withCheckedThrowingContinuation { continuation in - Task.detached { + Task.detached { [weak self] in + guard let self else { return } + var zoneIDs = Set() zoneIDs.insert(self.outlineZone.zoneID) @@ -631,7 +637,9 @@ private extension CloudKitManager { let imageSubscription = sharedDatabaseSubscription(recordType: Image.CloudKitRecord.recordType) return try await withCheckedThrowingContinuation { continuation in - Task.detached { + Task.detached { [weak self] in + guard let self else { return } + let op = CKModifySubscriptionsOperation(subscriptionsToSave: [outlineSubscription, rowSubscription, imageSubscription], subscriptionIDsToDelete: nil) op.qualityOfService = CloudKitOutlineZone.qualityOfService diff --git a/xcconfig/Zavala_target_common.xcconfig b/xcconfig/Zavala_target_common.xcconfig index 4db7d03a..afd17a37 100644 --- a/xcconfig/Zavala_target_common.xcconfig +++ b/xcconfig/Zavala_target_common.xcconfig @@ -1,3 +1,3 @@ MARKETING_VERSION = 3.0 -CURRENT_PROJECT_VERSION = 116 +CURRENT_PROJECT_VERSION = 117 SWIFT_OBJC_BRIDGING_HEADER = AppKitPlugin/BridgingHeader.h