From cf2bed1e696b41cd82450844e321125edd11826a Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sun, 26 Apr 2020 04:54:39 -0500 Subject: [PATCH] Delete all articles when the feed is deleted. --- .../CloudKit/CloudKitAccountDelegate.swift | 15 +++++-- .../CloudKit/CloudKitArticlesZone.swift | 16 ++++++- .../Account/CloudKit/CloudKitZone.swift | 45 ------------------- 3 files changed, 27 insertions(+), 49 deletions(-) diff --git a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift index d654b3829..8458f5b70 100644 --- a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift @@ -243,13 +243,22 @@ final class CloudKitAccountDelegate: AccountDelegate { } func removeWebFeed(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result) -> Void) { - refreshProgress.addToNumberOfTasksAndRemaining(1) + refreshProgress.addToNumberOfTasksAndRemaining(2) accountZone.removeWebFeed(feed, from: container) { result in self.refreshProgress.completeTask() switch result { case .success: - container.removeWebFeed(feed) - completion(.success(())) + self.articlesZone.deleteArticles(feed.url) { result in + self.refreshProgress.completeTask() + switch result { + case .success: + container.removeWebFeed(feed) + completion(.success(())) + case .failure(let error): + self.processAccountError(account, error) + completion(.failure(error)) + } + } case .failure(let error): self.processAccountError(account, error) completion(.failure(error)) diff --git a/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift b/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift index 37e7f14a7..c8b085cf4 100644 --- a/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift +++ b/Frameworks/Account/CloudKit/CloudKitArticlesZone.swift @@ -88,6 +88,21 @@ final class CloudKitArticlesZone: CloudKitZone { saveIfNew(records, completion: completion) } + func deleteArticles(_ webFeedURL: String, completion: @escaping ((Result) -> Void)) { + let predicate = NSPredicate(format: "webFeedURL = %@", webFeedURL) + let ckQuery = CKQuery(recordType: CloudKitArticle.recordType, predicate: predicate) + + query(ckQuery) { result in + switch result { + case .success(let records): + let recordIDs = records.map { $0.recordID } + self.delete(recordIDs: recordIDs, completion: completion) + case .failure(let error): + completion(.failure(error)) + } + } + } + func deleteArticles(_ articles: Set
, completion: @escaping ((Result) -> Void)) { guard !articles.isEmpty else { completion(.success(())) @@ -208,5 +223,4 @@ private extension CloudKitArticlesZone { return records } - } diff --git a/Frameworks/Account/CloudKit/CloudKitZone.swift b/Frameworks/Account/CloudKit/CloudKitZone.swift index 627f61f90..2e5e8e09b 100644 --- a/Frameworks/Account/CloudKit/CloudKitZone.swift +++ b/Frameworks/Account/CloudKit/CloudKitZone.swift @@ -121,51 +121,6 @@ extension CloudKitZone { } } } - - /// Checks to see if the record described in the query exists by retrieving only the testField parameter field. - func exists(_ query: CKQuery, completion: @escaping (Result) -> Void) { - var recordFound = false - let op = CKQueryOperation(query: query) - op.desiredKeys = ["creationDate"] - - op.recordFetchedBlock = { record in - recordFound = true - } - - op.queryCompletionBlock = { [weak self] (_, error) in - switch CloudKitZoneResult.resolve(error) { - case .success: - DispatchQueue.main.async { - completion(.success(recordFound)) - } - case .zoneNotFound: - self?.createZoneRecord() { result in - switch result { - case .success: - self?.exists(query, completion: completion) - case .failure(let error): - DispatchQueue.main.async { - completion(.failure(error)) - } - } - } - case .retry(let timeToWait): - self?.retryIfPossible(after: timeToWait) { - self?.exists(query, completion: completion) - } - case .userDeletedZone: - DispatchQueue.main.async { - completion(.failure(CloudKitZoneError.userDeletedZone)) - } - default: - DispatchQueue.main.async { - completion(.failure(CloudKitError(error!))) - } - } - } - - database?.add(op) - } /// Issue a CKQuery and return the resulting CKRecords.s func query(_ query: CKQuery, completion: @escaping (Result<[CKRecord], Error>) -> Void) {