Fix potential CloudKit crash where the database record might not come back on a query

This commit is contained in:
Maurice Parker 2020-06-16 04:00:58 -05:00
parent 50508446bb
commit cc69dd699f
2 changed files with 14 additions and 2 deletions

View File

@ -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

View File

@ -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()