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()
refresher.refreshFeeds(refresherWebFeeds) { refresherNewArticles, refresherUpdatedArticles, refresherDeletedArticles in
self.storeArticleChanges(new: refresherNewArticles, updated: refresherUpdatedArticles, deleted: refresherDeletedArticles)
refresher.refreshFeeds(refresherWebFeeds) {
group.leave()
}
@ -788,6 +787,7 @@ private extension CloudKitAccountDelegate {
extension CloudKitAccountDelegate: LocalAccountRefresherDelegate {
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) {

View File

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

View File

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