From a7ca5bfa79858fde95069b6c779d7ab23ce33fca Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Thu, 23 Apr 2020 16:39:09 -0500 Subject: [PATCH] Delete old articles and article statuses from CloudKit --- .../CloudKit/CloudKitAccountDelegate.swift | 9 +++++---- .../Account/CloudKit/CloudKitArticlesZone.swift | 15 +++++++++++++++ Frameworks/Account/CloudKit/CloudKitZone.swift | 4 ++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift index 2c8708079..f352c35cb 100644 --- a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift @@ -628,12 +628,13 @@ private extension CloudKitAccountDelegate { extension CloudKitAccountDelegate: LocalAccountRefresherDelegate { func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess articleChanges: ArticleChanges, completion: @escaping () -> Void) { - if let newArticles = articleChanges.newArticles { - articlesZone.sendNewArticles(newArticles) { _ in + let newArticles = articleChanges.newArticles ?? Set
() + let deletedArticles = articleChanges.deletedArticles ?? Set
() + + articlesZone.deleteArticles(deletedArticles) { _ in + self.articlesZone.sendNewArticles(newArticles) { _ in completion() } - } else { - completion() } } diff --git a/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift b/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift index 1f71d3c46..36eddda80 100644 --- a/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift +++ b/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift @@ -82,10 +82,25 @@ final class CloudKitArticlesZone: CloudKitZone { } func sendNewArticles(_ articles: Set
, completion: @escaping ((Result) -> Void)) { + guard !articles.isEmpty else { + completion(.success(())) + return + } + let records = makeNewStatusRecords(articles) saveIfNew(records, completion: completion) } + func deleteArticles(_ articles: Set
, completion: @escaping ((Result) -> Void)) { + guard !articles.isEmpty else { + completion(.success(())) + return + } + + let recordIDs = articles.map { CKRecord.ID(recordName: $0.articleID, zoneID: Self.zoneID) } + delete(recordIDs: recordIDs, completion: completion) + } + func sendArticleStatus(_ syncStatuses: [SyncStatus], articles: Set
, completion: @escaping ((Result) -> Void)) { var records = makeStatusRecords(syncStatuses, articles) diff --git a/Frameworks/Account/CloudKit/CloudKitZone.swift b/Frameworks/Account/CloudKit/CloudKitZone.swift index 3bd02c1ee..e0cc2c891 100644 --- a/Frameworks/Account/CloudKit/CloudKitZone.swift +++ b/Frameworks/Account/CloudKit/CloudKitZone.swift @@ -371,6 +371,10 @@ extension CloudKitZone { modify(recordsToSave: [], recordIDsToDelete: [recordID], completion: completion) } + func delete(recordIDs: [CKRecord.ID], completion: @escaping (Result) -> Void) { + modify(recordsToSave: [], recordIDsToDelete: recordIDs, completion: completion) + } + /// Delete a CKRecord using its externalID func delete(externalID: String?, completion: @escaping (Result) -> Void) { guard let externalID = externalID else {