From 41727a3ffdd7168ea55a2cbf95c7584a600fb89b Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Tue, 28 Apr 2020 12:31:53 -0500 Subject: [PATCH] Restrict status sync so that new ones can't start while a long running one is in progress. --- .../CloudKit/CloudKitAccountDelegate.swift | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift index 8ac24799c..00fba6788 100644 --- a/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Frameworks/Account/CloudKit/CloudKitAccountDelegate.swift @@ -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)) { + 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)) { + 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: