Delete old articles and article statuses from CloudKit

This commit is contained in:
Maurice Parker 2020-04-23 16:39:09 -05:00
parent ac45ba57a9
commit a7ca5bfa79
3 changed files with 24 additions and 4 deletions

View File

@ -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<Article>()
let deletedArticles = articleChanges.deletedArticles ?? Set<Article>()
articlesZone.deleteArticles(deletedArticles) { _ in
self.articlesZone.sendNewArticles(newArticles) { _ in
completion()
}
} else {
completion()
}
}

View File

@ -82,10 +82,25 @@ final class CloudKitArticlesZone: CloudKitZone {
}
func sendNewArticles(_ articles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) {
guard !articles.isEmpty else {
completion(.success(()))
return
}
let records = makeNewStatusRecords(articles)
saveIfNew(records, completion: completion)
}
func deleteArticles(_ articles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> 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<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) {
var records = makeStatusRecords(syncStatuses, articles)

View File

@ -371,6 +371,10 @@ extension CloudKitZone {
modify(recordsToSave: [], recordIDsToDelete: [recordID], completion: completion)
}
func delete(recordIDs: [CKRecord.ID], completion: @escaping (Result<Void, Error>) -> Void) {
modify(recordsToSave: [], recordIDsToDelete: recordIDs, completion: completion)
}
/// Delete a CKRecord using its externalID
func delete(externalID: String?, completion: @escaping (Result<Void, Error>) -> Void) {
guard let externalID = externalID else {