From cc69dd699fa05f9e19e5d0088a32960e548209dc Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 16 Jun 2020 04:00:58 -0500 Subject: [PATCH] Fix potential CloudKit crash where the database record might not come back on a query --- .../CloudKit/CloudKitArticleStatusUpdate.swift | 12 ++++++++++++ .../CloudKit/CloudKitSendStatusOperation.swift | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Frameworks/Account/CloudKit/CloudKitArticleStatusUpdate.swift b/Frameworks/Account/CloudKit/CloudKitArticleStatusUpdate.swift index 65048cb9d..9bad6d6d0 100644 --- a/Frameworks/Account/CloudKit/CloudKitArticleStatusUpdate.swift +++ b/Frameworks/Account/CloudKit/CloudKitArticleStatusUpdate.swift @@ -23,6 +23,18 @@ struct CloudKitArticleStatusUpdate { var statuses: [SyncStatus] var article: Article? + init?(articleID: String, statuses: [SyncStatus], article: Article?) { + self.articleID = articleID + self.statuses = statuses + self.article = article + + let rec = record + // This is an invalid status update. The article is required for new and all + if article == nil && (rec == .all || rec == .new) { + return nil + } + } + var record: Record { if statuses.contains(where: { $0.key == .deleted }) { return .delete diff --git a/Frameworks/Account/CloudKit/CloudKitSendStatusOperation.swift b/Frameworks/Account/CloudKit/CloudKitSendStatusOperation.swift index d94dbaea7..3d5aefabf 100644 --- a/Frameworks/Account/CloudKit/CloudKitSendStatusOperation.swift +++ b/Frameworks/Account/CloudKit/CloudKitSendStatusOperation.swift @@ -110,14 +110,14 @@ private extension CloudKitSendStatusOperation { let articlesDict = articles.reduce(into: [String: Article]()) { result, article in result[article.articleID] = article } - let statusUpdates = syncStatusesDict.map { (key, value) in + let statusUpdates = syncStatusesDict.compactMap { (key, value) in return CloudKitArticleStatusUpdate(articleID: key, statuses: value, article: articlesDict[key]) } articlesZone.modifyArticles(statusUpdates) { result in switch result { case .success: - self.database.deleteSelectedForProcessing(syncStatuses.map({ $0.articleID })) { _ in + self.database.deleteSelectedForProcessing(statusUpdates.map({ $0.articleID })) { _ in // Don't clear the last one since we might have had additional ticks added if self.showProgress && self.refreshProgress?.numberRemaining ?? 0 > 1 { self.refreshProgress?.completeTask()