Skip to content

Commit

Permalink
Fix the race condition that could cause rows to come back after being…
Browse files Browse the repository at this point in the history
… deleted which caused corrupt outlines
  • Loading branch information
vincode-io committed Sep 11, 2024
1 parent 8517ae0 commit aa83418
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public struct CloudKitActionRequest: Codable, Hashable, Equatable {
var zoneID: CKRecordZone.ID {
return CKRecordZone.ID(zoneName: zoneName, ownerName: zoneOwner)
}

var recordID: CKRecord.ID {
return CKRecord.ID(recordName: id.description, zoneID: zoneID)
}

enum CodingKeys: String, CodingKey {
case zoneName = "zoneName"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ public class CloudKitManager {
}
}

func loadRequests() async -> Set<CloudKitActionRequest> {
await requestsSemaphore.wait()
let requests = CloudKitActionRequest.load()
requestsSemaphore.signal()
return requests ?? []
}

func receiveRemoteNotification(userInfo: [AnyHashable : Any]) async {
restartWorkTask()

Expand Down Expand Up @@ -474,7 +481,7 @@ private extension CloudKitManager {

func addDelete(_ request: CloudKitActionRequest) {
let zoneID = request.zoneID
let recordID = CKRecord.ID(recordName: request.id.description, zoneID: zoneID)
let recordID = request.recordID
addDelete(zoneID, recordID)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class CloudKitOutlineZoneDelegate: VCKZoneDelegate {
}

func cloudKitDidModify(changed: [CKRecord], deleted: [CloudKitRecordKey]) async throws {
let requests = await account?.cloudKitManager?.loadRequests() ?? []
let requestRecordIDs = Set(requests.map({ $0.recordID }))

var updates = [EntityID: CloudKitOutlineUpdate]()
var shareUpdates = [(CKRecord.ID, CKShare?)]()

Expand All @@ -42,6 +45,10 @@ class CloudKitOutlineZoneDelegate: VCKZoneDelegate {
}

for deletedRecordKey in deleted {
guard !requestRecordIDs.contains(deletedRecordKey.recordID) else {
continue
}

if deletedRecordKey.recordType == CKRecord.SystemType.share {
shareUpdates.append((deletedRecordKey.recordID, nil))
} else {
Expand All @@ -62,6 +69,10 @@ class CloudKitOutlineZoneDelegate: VCKZoneDelegate {
}

for changedRecord in changed {
guard !requestRecordIDs.contains(changedRecord.recordID) else {
continue
}

if let shareRecord = changedRecord as? CKShare {
shareUpdates.append((shareRecord.recordID, shareRecord))
} else {
Expand Down

0 comments on commit aa83418

Please sign in to comment.