Change so that sync database writes happen immediately for the local account refresher

This commit is contained in:
Maurice Parker 2020-05-08 02:32:13 -05:00
parent ec499b1347
commit 1e29809d94
3 changed files with 23 additions and 28 deletions

View File

@ -567,8 +567,7 @@ private extension CloudKitAccountDelegate {
} }
group.enter() group.enter()
refresher.refreshFeeds(refresherWebFeeds) { refresherNewArticles, refresherUpdatedArticles, refresherDeletedArticles in refresher.refreshFeeds(refresherWebFeeds) {
self.storeArticleChanges(new: refresherNewArticles, updated: refresherUpdatedArticles, deleted: refresherDeletedArticles)
group.leave() group.leave()
} }
@ -788,6 +787,7 @@ private extension CloudKitAccountDelegate {
extension CloudKitAccountDelegate: LocalAccountRefresherDelegate { extension CloudKitAccountDelegate: LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess articleChanges: ArticleChanges, completion: @escaping () -> Void) { func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess articleChanges: ArticleChanges, completion: @escaping () -> Void) {
self.storeArticleChanges(new: articleChanges.newArticles, updated: articleChanges.updatedArticles, deleted: articleChanges.deletedArticles)
} }
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) { func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) {

View File

@ -52,12 +52,12 @@ final class LocalAccountDelegate: AccountDelegate {
var refresherWebFeeds = Set<WebFeed>() var refresherWebFeeds = Set<WebFeed>()
let webFeeds = account.flattenedWebFeeds() let webFeeds = account.flattenedWebFeeds()
refreshProgress.addToNumberOfTasksAndRemaining(webFeeds.count)
let group = DispatchGroup() let group = DispatchGroup()
for webFeed in webFeeds { for webFeed in webFeeds {
if let components = URLComponents(string: webFeed.url), let feedProvider = FeedProviderManager.shared.best(for: components) { if let components = URLComponents(string: webFeed.url), let feedProvider = FeedProviderManager.shared.best(for: components) {
refreshProgress.addToNumberOfTasksAndRemaining(1)
group.enter() group.enter()
feedProvider.refresh(webFeed) { result in feedProvider.refresh(webFeed) { result in
switch result { switch result {
@ -77,13 +77,13 @@ final class LocalAccountDelegate: AccountDelegate {
} }
} }
refreshProgress.addToNumberOfTasksAndRemaining(refresherWebFeeds.count)
group.enter() group.enter()
refresher?.refreshFeeds(refresherWebFeeds) { _, _, _ in refresher?.refreshFeeds(refresherWebFeeds) {
group.leave() group.leave()
} }
group.notify(queue: DispatchQueue.main) { group.notify(queue: DispatchQueue.main) {
self.refreshProgress.clear()
account.metadata.lastArticleFetchEndTime = Date() account.metadata.lastArticleFetchEndTime = Date()
completion(.success(())) completion(.success(()))
} }
@ -234,13 +234,16 @@ final class LocalAccountDelegate: AccountDelegate {
} }
extension LocalAccountDelegate: LocalAccountRefresherDelegate { extension LocalAccountDelegate: LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: ArticleChanges, completion: @escaping () -> Void) {
completion()
}
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) { func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) {
refreshProgress.completeTask() refreshProgress.completeTask()
} }
func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher) { func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher) {
self.refreshProgress.clear()
} }
} }

View File

@ -14,16 +14,14 @@ import Articles
import ArticlesDatabase import ArticlesDatabase
protocol LocalAccountRefresherDelegate { protocol LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: ArticleChanges, completion: @escaping () -> Void)
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed)
func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher) func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher)
} }
final class LocalAccountRefresher { final class LocalAccountRefresher {
var newArticles = Set<Article>() private var completion: (() -> Void)? = nil
var updatedArticles = Set<Article>()
var deletedArticles = Set<Article>()
private var completion: ((Set<Article>, Set<Article>, Set<Article>) -> Void)?
private var isSuspended = false private var isSuspended = false
var delegate: LocalAccountRefresherDelegate? var delegate: LocalAccountRefresherDelegate?
@ -31,9 +29,9 @@ final class LocalAccountRefresher {
return DownloadSession(delegate: self) return DownloadSession(delegate: self)
}() }()
public func refreshFeeds(_ feeds: Set<WebFeed>, completion: ((Set<Article>, Set<Article>, Set<Article>) -> Void)? = nil) { public func refreshFeeds(_ feeds: Set<WebFeed>, completion: (() -> Void)? = nil) {
guard !feeds.isEmpty else { guard !feeds.isEmpty else {
completion?(Set<Article>(), Set<Article>(), Set<Article>()) completion?()
return return
} }
self.completion = completion self.completion = completion
@ -105,18 +103,14 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
account.update(feed, with: parsedFeed) { result in account.update(feed, with: parsedFeed) { result in
if case .success(let articleChanges) = result { if case .success(let articleChanges) = result {
self.delegate?.localAccountRefresher(self, didProcess: articleChanges) {
self.newArticles.formUnion(articleChanges.newArticles ?? Set<Article>()) if let httpResponse = response as? HTTPURLResponse {
self.updatedArticles.formUnion(articleChanges.updatedArticles ?? Set<Article>()) feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse)
self.deletedArticles.formUnion(articleChanges.deletedArticles ?? Set<Article>()) }
feed.contentHash = dataHash
if let httpResponse = response as? HTTPURLResponse { completion()
feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse) self.delegate?.localAccountRefresher(self, requestCompletedFor: feed)
} }
feed.contentHash = dataHash
completion()
self.delegate?.localAccountRefresher(self, requestCompletedFor: feed)
} else { } else {
completion() completion()
self.delegate?.localAccountRefresher(self, requestCompletedFor: feed) self.delegate?.localAccountRefresher(self, requestCompletedFor: feed)
@ -171,10 +165,8 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
} }
func downloadSessionDidCompleteDownloadObjects(_ downloadSession: DownloadSession) { func downloadSessionDidCompleteDownloadObjects(_ downloadSession: DownloadSession) {
completion?(newArticles, updatedArticles, deletedArticles) completion?()
completion = nil completion = nil
newArticles = Set<Article>()
deletedArticles = Set<Article>()
delegate?.localAccountRefresherDidFinish(self) delegate?.localAccountRefresherDidFinish(self)
} }