Add CloudKit deletes

This commit is contained in:
Maurice Parker 2020-04-29 19:13:50 -05:00
parent 0b49acfabc
commit 095c8575f3
4 changed files with 61 additions and 10 deletions

View File

@ -854,6 +854,15 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
markAndFetchNew(articleIDs: articleIDs, statusKey: .starred, flag: false, completion: completion)
}
// Delete the articles associated with the given set of articleIDs
func delete(articleIDs: Set<String>, completion: DatabaseCompletionBlock? = nil) {
guard !articleIDs.isEmpty else {
completion?(nil)
return
}
database.delete(articleIDs: articleIDs, completion: completion)
}
/// Empty caches that can reasonably be emptied. Call when the app goes in the background, for instance.
func emptyCaches() {
database.emptyCaches()

View File

@ -38,11 +38,13 @@ class CloudKitArticlesZoneDelegate: CloudKitZoneDelegate {
self.database.selectPendingStarredStatusArticleIDs() { result in
switch result {
case .success(let pendingStarredStatusArticleIDs):
self.process(records: changed,
pendingReadStatusArticleIDs: pendingReadStatusArticleIDs,
pendingStarredStatusArticleIDs: pendingStarredStatusArticleIDs,
completion: completion)
self.delete(recordKeys: deleted, pendingStarredStatusArticleIDs: pendingStarredStatusArticleIDs) {
self.update(records: changed,
pendingReadStatusArticleIDs: pendingReadStatusArticleIDs,
pendingStarredStatusArticleIDs: pendingStarredStatusArticleIDs,
completion: completion)
}
case .failure(let error):
os_log(.error, log: self.log, "Error occurred geting pending starred records: %@", error.localizedDescription)
@ -59,8 +61,20 @@ class CloudKitArticlesZoneDelegate: CloudKitZoneDelegate {
}
private extension CloudKitArticlesZoneDelegate {
func process(records: [CKRecord], pendingReadStatusArticleIDs: Set<String>, pendingStarredStatusArticleIDs: Set<String>, completion: @escaping (Result<Void, Error>) -> Void) {
func delete(recordKeys: [CloudKitRecordKey], pendingStarredStatusArticleIDs: Set<String>, completion: @escaping () -> Void) {
let receivedRecordIDs = recordKeys.filter({ $0.recordType == CloudKitArticlesZone.CloudKitArticleStatus.recordType }).map({ $0.recordID })
let receivedArticleIDs = Set(receivedRecordIDs.map({ stripPrefix($0.externalID) }))
let deletableArticleIDs = receivedArticleIDs.subtracting(pendingStarredStatusArticleIDs)
database.deleteSelectedForProcessing(Array(deletableArticleIDs)) { _ in
self.account?.delete(articleIDs: deletableArticleIDs) { _ in
completion()
}
}
}
func update(records: [CKRecord], pendingReadStatusArticleIDs: Set<String>, pendingStarredStatusArticleIDs: Set<String>, completion: @escaping (Result<Void, Error>) -> Void) {
let receivedUnreadArticleIDs = Set(records.filter({ $0[CloudKitArticlesZone.CloudKitArticleStatus.Fields.read] == "0" }).map({ stripPrefix($0.externalID) }))
let receivedReadArticleIDs = Set(records.filter({ $0[CloudKitArticlesZone.CloudKitArticleStatus.Fields.read] == "1" }).map({ stripPrefix($0.externalID) }))
@ -75,7 +89,7 @@ private extension CloudKitArticlesZoneDelegate {
let group = DispatchGroup()
group.enter()
account?.markAsUnread(updateableUnreadArticleIDs) { result in
account?.markAsUnread(updateableUnreadArticleIDs) { _ in
group.leave()
}

View File

@ -201,7 +201,7 @@ public final class ArticlesDatabase {
articlesTable.fetchStarredAndUnreadCount(webFeedIDs, completion)
}
// MARK: - Saving and Updating Articles
// MARK: - Saving, Updating, and Deleting Articles
/// Update articles and save new ones  for feed-based systems (local and iCloud).
public func update(with parsedItems: Set<ParsedItem>, webFeedID: String, completion: @escaping UpdateArticlesCompletionBlock) {
@ -215,6 +215,11 @@ public final class ArticlesDatabase {
articlesTable.update(webFeedIDsAndItems, defaultRead, completion)
}
/// Delete articles
public func delete(articleIDs: Set<String>, completion: DatabaseCompletionBlock?) {
articlesTable.delete(articleIDs: articleIDs, completion: completion)
}
// MARK: - Status
/// Fetch the articleIDs of unread articles in feeds specified by webFeedIDs.

View File

@ -170,7 +170,7 @@ final class ArticlesTable: DatabaseTable {
return nil
}
// MARK: - Updating
// MARK: - Updating and Deleting
func update(_ parsedItems: Set<ParsedItem>, _ webFeedID: String, _ completion: @escaping UpdateArticlesCompletionBlock) {
precondition(retentionStyle == .feedBased)
@ -318,6 +318,29 @@ final class ArticlesTable: DatabaseTable {
}
}
public func delete(articleIDs: Set<String>, completion: DatabaseCompletionBlock?) {
self.queue.runInTransaction { (databaseResult) in
func makeDatabaseCalls(_ database: FMDatabase) {
self.removeArticles(articleIDs, database)
DispatchQueue.main.async {
completion?(nil)
}
}
switch databaseResult {
case .success(let database):
makeDatabaseCalls(database)
case .failure(let databaseError):
DispatchQueue.main.async {
completion?(databaseError)
}
}
}
}
// MARK: - Unread Counts
func fetchUnreadCount(_ webFeedIDs: Set<String>, _ since: Date, _ completion: @escaping SingleUnreadCountCompletionBlock) {