Delete all articles when the feed is deleted.

This commit is contained in:
Maurice Parker 2020-04-26 04:54:39 -05:00
parent 71788d8f69
commit cf2bed1e69
3 changed files with 27 additions and 49 deletions

View File

@ -243,13 +243,22 @@ final class CloudKitAccountDelegate: AccountDelegate {
} }
func removeWebFeed(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void) { func removeWebFeed(for account: Account, with feed: WebFeed, from container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(1) refreshProgress.addToNumberOfTasksAndRemaining(2)
accountZone.removeWebFeed(feed, from: container) { result in accountZone.removeWebFeed(feed, from: container) { result in
self.refreshProgress.completeTask() self.refreshProgress.completeTask()
switch result { switch result {
case .success: case .success:
container.removeWebFeed(feed) self.articlesZone.deleteArticles(feed.url) { result in
completion(.success(())) 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): case .failure(let error):
self.processAccountError(account, error) self.processAccountError(account, error)
completion(.failure(error)) completion(.failure(error))

View File

@ -88,6 +88,21 @@ final class CloudKitArticlesZone: CloudKitZone {
saveIfNew(records, completion: completion) saveIfNew(records, completion: completion)
} }
func deleteArticles(_ webFeedURL: String, completion: @escaping ((Result<Void, Error>) -> 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<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) { func deleteArticles(_ articles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) {
guard !articles.isEmpty else { guard !articles.isEmpty else {
completion(.success(())) completion(.success(()))
@ -208,5 +223,4 @@ private extension CloudKitArticlesZone {
return records return records
} }
} }

View File

@ -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<Bool, Error>) -> 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 /// Issue a CKQuery and return the resulting CKRecords.s
func query(_ query: CKQuery, completion: @escaping (Result<[CKRecord], Error>) -> Void) { func query(_ query: CKQuery, completion: @escaping (Result<[CKRecord], Error>) -> Void) {