Make two more Feedly operations compatible with DatabaseQueue changes.

This commit is contained in:
Brent Simmons 2019-12-16 17:03:05 -08:00
parent 3883d24ebe
commit 9516392c61
2 changed files with 11 additions and 32 deletions

View File

@ -34,7 +34,7 @@ final class FeedlySetStarredArticlesOperation: FeedlyOperation {
account.fetchStarredArticleIDs { (articleIDsResult) in account.fetchStarredArticleIDs { (articleIDsResult) in
if let localStarredArticleIDs = try? articleIDsResult.get() { if let localStarredArticleIDs = try? articleIDsResult.get() {
self.didFetchStarredArticleIDs(localStarredArticleIDs) self.processStarredArticleIDs(localStarredArticleIDs)
} }
else { else {
self.didFinish() self.didFinish()
@ -45,41 +45,20 @@ final class FeedlySetStarredArticlesOperation: FeedlyOperation {
private extension FeedlySetStarredArticlesOperation { private extension FeedlySetStarredArticlesOperation {
func didFetchStarredArticleIDs(_ localStarredArticleIDs: Set<String>) { func processStarredArticleIDs(_ localStarredArticleIDs: Set<String>) {
guard !isCancelled else { guard !isCancelled else {
didFinish() didFinish()
return return
} }
let group = DispatchGroup()
let remoteStarredArticleIds = allStarredEntryIdsProvider.entryIds
// Mark articles as starred
let deltaStarredArticleIDs = remoteStarredArticleIds.subtracting(localStarredArticleIDs)
let markStarredArticles = account.fetchArticles(.articleIDs(deltaStarredArticleIDs))
account.update(markStarredArticles, statusKey: .starred, flag: true)
// Save any starred statuses for articles we haven't yet received // Mark as starred
let markStarredArticleIDs = Set(markStarredArticles.map { $0.articleID }) let remoteStarredArticleIDs = allStarredEntryIdsProvider.entryIds
let missingStarredArticleIDs = deltaStarredArticleIDs.subtracting(markStarredArticleIDs) account.mark(articleIDs: remoteStarredArticleIDs, statusKey: .starred, flag: true)
group.enter()
account.ensureStatuses(missingStarredArticleIDs, true, .starred, true) {
group.leave()
}
// Mark articles as unstarred // Mark as unstarred
let deltaUnstarredArticleIDs = localStarredArticleIDs.subtracting(remoteStarredArticleIds) let deltaUnstarredArticleIDs = localStarredArticleIDs.subtracting(remoteStarredArticleIDs)
let markUnstarredArticles = account.fetchArticles(.articleIDs(deltaUnstarredArticleIDs)) account.mark(articleIDs: deltaUnstarredArticleIDs, statusKey: .starred, flag: false)
account.update(markUnstarredArticles, statusKey: .starred, flag: false)
// Save any unstarred statuses for articles we haven't yet received didFinish()
let markUnstarredArticleIDs = Set(markUnstarredArticles.map { $0.articleID })
let missingUnstarredArticleIDs = deltaUnstarredArticleIDs.subtracting(markUnstarredArticleIDs)
group.enter()
account.ensureStatuses(missingUnstarredArticleIDs, true, .starred, false) {
group.leave()
}
group.notify(queue: .main, execute: didFinish)
} }
} }

View File

@ -23,7 +23,7 @@ final class FeedlyUpdateAccountFeedsWithItemsOperation: FeedlyOperation {
} }
override func main() { override func main() {
assert(Thread.isMainThread) // Needs to be on main thread because Feed is a main-thread-only model type. precondition(Thread.isMainThread) // Needs to be on main thread because Feed is a main-thread-only model type.
guard !isCancelled else { guard !isCancelled else {
didFinish() didFinish()
return return
@ -31,7 +31,7 @@ final class FeedlyUpdateAccountFeedsWithItemsOperation: FeedlyOperation {
let webFeedIDsAndItems = organisedItemsProvider.parsedItemsKeyedByFeedId let webFeedIDsAndItems = organisedItemsProvider.parsedItemsKeyedByFeedId
account.update(webFeedIDsAndItems: webFeedIDsAndItems, defaultRead: true) { account.update(webFeedIDsAndItems: webFeedIDsAndItems, defaultRead: true) { _ in
os_log(.debug, log: self.log, "Updated %i feeds for \"%@\"", webFeedIDsAndItems.count, self.organisedItemsProvider.providerName) os_log(.debug, log: self.log, "Updated %i feeds for \"%@\"", webFeedIDsAndItems.count, self.organisedItemsProvider.providerName)
self.didFinish() self.didFinish()
} }