Restrict status sync so that new ones can't start while a long running one is in progress.

This commit is contained in:
Maurice Parker 2020-04-28 12:31:53 -05:00
parent 4f749cb381
commit 41727a3ffd
1 changed files with 20 additions and 1 deletions

View File

@ -53,7 +53,8 @@ final class CloudKitAccountDelegate: AccountDelegate {
var accountMetadata: AccountMetadata?
var refreshProgress = DownloadProgress(numberOfTasks: 0)
var statusProcessRunning = false
init(dataFolder: String) {
accountZone = CloudKitAccountZone(container: container)
articlesZone = CloudKitArticlesZone(container: container)
@ -97,12 +98,19 @@ final class CloudKitAccountDelegate: AccountDelegate {
}
func sendArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
guard !statusProcessRunning else {
completion(.success(()))
return
}
statusProcessRunning = true
os_log(.debug, log: log, "Sending article statuses...")
database.selectForProcessing { result in
func processStatuses(_ syncStatuses: [SyncStatus]) {
guard syncStatuses.count > 0 else {
self.statusProcessRunning = false
completion(.success(()))
return
}
@ -122,11 +130,13 @@ final class CloudKitAccountDelegate: AccountDelegate {
case .success:
self.database.deleteSelectedForProcessing(syncStatuses.map({ $0.articleID })) { _ in
os_log(.debug, log: self.log, "Done sending article statuses.")
self.statusProcessRunning = false
completion(.success(()))
}
case .failure(let error):
self.database.resetSelectedForProcessing(syncStatuses.map({ $0.articleID })) { _ in
self.processAccountError(account, error)
self.statusProcessRunning = false
completion(.failure(error))
}
}
@ -138,6 +148,7 @@ final class CloudKitAccountDelegate: AccountDelegate {
case .success(let articles):
processWithArticles(articles)
case .failure(let databaseError):
self.statusProcessRunning = false
completion(.failure(databaseError))
}
@ -149,6 +160,7 @@ final class CloudKitAccountDelegate: AccountDelegate {
case .success(let syncStatuses):
processStatuses(syncStatuses)
case .failure(let databaseError):
self.statusProcessRunning = false
completion(.failure(databaseError))
}
}
@ -156,9 +168,16 @@ final class CloudKitAccountDelegate: AccountDelegate {
func refreshArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
guard !statusProcessRunning else {
completion(.success(()))
return
}
statusProcessRunning = true
os_log(.debug, log: log, "Refreshing article statuses...")
articlesZone.refreshArticles() { result in
self.statusProcessRunning = false
os_log(.debug, log: self.log, "Done refreshing article statuses.")
switch result {
case .success: