Restrict status sync so that new ones can't start while a long running one is in progress.
This commit is contained in:
parent
4f749cb381
commit
41727a3ffd
|
@ -53,7 +53,8 @@ final class CloudKitAccountDelegate: AccountDelegate {
|
||||||
var accountMetadata: AccountMetadata?
|
var accountMetadata: AccountMetadata?
|
||||||
|
|
||||||
var refreshProgress = DownloadProgress(numberOfTasks: 0)
|
var refreshProgress = DownloadProgress(numberOfTasks: 0)
|
||||||
|
var statusProcessRunning = false
|
||||||
|
|
||||||
init(dataFolder: String) {
|
init(dataFolder: String) {
|
||||||
accountZone = CloudKitAccountZone(container: container)
|
accountZone = CloudKitAccountZone(container: container)
|
||||||
articlesZone = CloudKitArticlesZone(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)) {
|
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...")
|
os_log(.debug, log: log, "Sending article statuses...")
|
||||||
|
|
||||||
database.selectForProcessing { result in
|
database.selectForProcessing { result in
|
||||||
|
|
||||||
func processStatuses(_ syncStatuses: [SyncStatus]) {
|
func processStatuses(_ syncStatuses: [SyncStatus]) {
|
||||||
guard syncStatuses.count > 0 else {
|
guard syncStatuses.count > 0 else {
|
||||||
|
self.statusProcessRunning = false
|
||||||
completion(.success(()))
|
completion(.success(()))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -122,11 +130,13 @@ final class CloudKitAccountDelegate: AccountDelegate {
|
||||||
case .success:
|
case .success:
|
||||||
self.database.deleteSelectedForProcessing(syncStatuses.map({ $0.articleID })) { _ in
|
self.database.deleteSelectedForProcessing(syncStatuses.map({ $0.articleID })) { _ in
|
||||||
os_log(.debug, log: self.log, "Done sending article statuses.")
|
os_log(.debug, log: self.log, "Done sending article statuses.")
|
||||||
|
self.statusProcessRunning = false
|
||||||
completion(.success(()))
|
completion(.success(()))
|
||||||
}
|
}
|
||||||
case .failure(let error):
|
case .failure(let error):
|
||||||
self.database.resetSelectedForProcessing(syncStatuses.map({ $0.articleID })) { _ in
|
self.database.resetSelectedForProcessing(syncStatuses.map({ $0.articleID })) { _ in
|
||||||
self.processAccountError(account, error)
|
self.processAccountError(account, error)
|
||||||
|
self.statusProcessRunning = false
|
||||||
completion(.failure(error))
|
completion(.failure(error))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -138,6 +148,7 @@ final class CloudKitAccountDelegate: AccountDelegate {
|
||||||
case .success(let articles):
|
case .success(let articles):
|
||||||
processWithArticles(articles)
|
processWithArticles(articles)
|
||||||
case .failure(let databaseError):
|
case .failure(let databaseError):
|
||||||
|
self.statusProcessRunning = false
|
||||||
completion(.failure(databaseError))
|
completion(.failure(databaseError))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -149,6 +160,7 @@ final class CloudKitAccountDelegate: AccountDelegate {
|
||||||
case .success(let syncStatuses):
|
case .success(let syncStatuses):
|
||||||
processStatuses(syncStatuses)
|
processStatuses(syncStatuses)
|
||||||
case .failure(let databaseError):
|
case .failure(let databaseError):
|
||||||
|
self.statusProcessRunning = false
|
||||||
completion(.failure(databaseError))
|
completion(.failure(databaseError))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,9 +168,16 @@ final class CloudKitAccountDelegate: AccountDelegate {
|
||||||
|
|
||||||
|
|
||||||
func refreshArticleStatus(for account: Account, completion: @escaping ((Result<Void, Error>) -> Void)) {
|
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...")
|
os_log(.debug, log: log, "Refreshing article statuses...")
|
||||||
|
|
||||||
articlesZone.refreshArticles() { result in
|
articlesZone.refreshArticles() { result in
|
||||||
|
self.statusProcessRunning = false
|
||||||
os_log(.debug, log: self.log, "Done refreshing article statuses.")
|
os_log(.debug, log: self.log, "Done refreshing article statuses.")
|
||||||
switch result {
|
switch result {
|
||||||
case .success:
|
case .success:
|
||||||
|
|
Loading…
Reference in New Issue