From 7a134740ec56f2f9b1d4294e5bbf73ab036d629e Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 16 Dec 2019 10:50:13 -0800 Subject: [PATCH] Make SyncDatabase selectPendingCount async. --- .../FeedWranglerAccountDelegate.swift | 6 ++--- .../Feedbin/FeedbinAccountDelegate.swift | 13 ++++++----- .../Feedly/FeedlyAccountDelegate.swift | 10 ++++---- .../ReaderAPI/ReaderAPIAccountDelegate.swift | 6 +++-- Frameworks/SyncDatabase/SyncDatabase.swift | 8 +++---- Frameworks/SyncDatabase/SyncStatusTable.swift | 23 +++++++++++-------- 6 files changed, 37 insertions(+), 29 deletions(-) diff --git a/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift b/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift index ebb98a24c..ee0788e5a 100644 --- a/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift +++ b/Frameworks/Account/FeedWrangler/FeedWranglerAccountDelegate.swift @@ -412,9 +412,9 @@ final class FeedWranglerAccountDelegate: AccountDelegate { let syncStatuses = articles.map { SyncStatus(articleID: $0.articleID, key: statusKey, flag: flag)} database.insertStatuses(syncStatuses) - if database.selectPendingCount() > 0 { - sendArticleStatus(for: account) { _ in - // do it in the background + database.selectPendingCount { result in + if let count = try? result.get(), count > 0 { + self.sendArticleStatus(for: account) { _ in } } } diff --git a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift index cc2cc32fc..37b196a26 100644 --- a/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift +++ b/Frameworks/Account/Feedbin/FeedbinAccountDelegate.swift @@ -524,13 +524,14 @@ final class FeedbinAccountDelegate: AccountDelegate { return SyncStatus(articleID: article.articleID, key: statusKey, flag: flag) } database.insertStatuses(syncStatuses) - - if database.selectPendingCount() > 100 { - sendArticleStatus(for: account) { _ in } + + database.selectPendingCount { result in + if let count = try? result.get(), count > 100 { + self.sendArticleStatus(for: account) { _ in } + } } - - return account.update(articles, statusKey: statusKey, flag: flag) - + + return account.update(articles, statusKey: statusKey, flag: flag) } func accountDidInitialize(_ account: Account) { diff --git a/Frameworks/Account/Feedly/FeedlyAccountDelegate.swift b/Frameworks/Account/Feedly/FeedlyAccountDelegate.swift index 558ea038a..eff71d14e 100644 --- a/Frameworks/Account/Feedly/FeedlyAccountDelegate.swift +++ b/Frameworks/Account/Feedly/FeedlyAccountDelegate.swift @@ -484,11 +484,13 @@ final class FeedlyAccountDelegate: AccountDelegate { database.insertStatuses(syncStatuses) os_log(.debug, log: log, "Marking %@ as %@.", articles.map { $0.title }, syncStatuses) - - if database.selectPendingCount() > 100 { - sendArticleStatus(for: account) { _ in } + + database.selectPendingCount { result in + if let count = try? result.get(), count > 100 { + self.sendArticleStatus(for: account) { _ in } + } } - + return account.update(articles, statusKey: statusKey, flag: flag) } diff --git a/Frameworks/Account/ReaderAPI/ReaderAPIAccountDelegate.swift b/Frameworks/Account/ReaderAPI/ReaderAPIAccountDelegate.swift index d30aec4eb..f6445b2e6 100644 --- a/Frameworks/Account/ReaderAPI/ReaderAPIAccountDelegate.swift +++ b/Frameworks/Account/ReaderAPI/ReaderAPIAccountDelegate.swift @@ -402,8 +402,10 @@ final class ReaderAPIAccountDelegate: AccountDelegate { } database.insertStatuses(syncStatuses) - if database.selectPendingCount() > 100 { - sendArticleStatus(for: account) { _ in } + database.selectPendingCount { result in + if let count = try? result.get(), count > 100 { + self.sendArticleStatus(for: account) { _ in } + } } return account.update(articles, statusKey: statusKey, flag: flag) diff --git a/Frameworks/SyncDatabase/SyncDatabase.swift b/Frameworks/SyncDatabase/SyncDatabase.swift index baa95f0ee..b0a1570ee 100644 --- a/Frameworks/SyncDatabase/SyncDatabase.swift +++ b/Frameworks/SyncDatabase/SyncDatabase.swift @@ -33,11 +33,11 @@ public struct SyncDatabase { public func selectForProcessing() throws -> [SyncStatus] { return try syncStatusTable.selectForProcessing() } - - public func selectPendingCount() throws -> Int { - return try syncStatusTable.selectPendingCount() + + public func selectPendingCount(completion: @escaping DatabaseIntCompletionBlock) { + syncStatusTable.selectPendingCount(completion) } - + public func resetSelectedForProcessing(_ articleIDs: [String], completion: DatabaseCompletionBlock? = nil) { syncStatusTable.resetSelectedForProcessing(articleIDs, completion: completion) } diff --git a/Frameworks/SyncDatabase/SyncStatusTable.swift b/Frameworks/SyncDatabase/SyncStatusTable.swift index 0d5dc8041..2cb6c0133 100644 --- a/Frameworks/SyncDatabase/SyncStatusTable.swift +++ b/Frameworks/SyncDatabase/SyncStatusTable.swift @@ -50,16 +50,15 @@ struct SyncStatusTable: DatabaseTable { return statuses != nil ? Array(statuses!) : [SyncStatus]() } - func selectPendingCount() throws -> Int { - var count: Int = 0 - var error: DatabaseError? - - queue.runInDatabaseSync { databaseResult in + func selectPendingCount(_ completion: @escaping DatabaseIntCompletionBlock) { + queue.runInDatabase { databaseResult in + var count: Int = 0 + var error: DatabaseError? func makeDatabaseCall(_ database: FMDatabase) { let sql = "select count(*) from syncStatus" if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) { - count = numberWithCountResultSet(resultSet) + count = self.numberWithCountResultSet(resultSet) } } @@ -69,12 +68,16 @@ struct SyncStatusTable: DatabaseTable { case .failure(let databaseError): error = databaseError } - } - if let error = error { - throw(error) + DispatchQueue.main.async { + if let error = error { + completion(.failure(error)) + } + else { + completion(.success(count)) + } + } } - return count } func resetSelectedForProcessing(_ articleIDs: [String], completion: DatabaseCompletionBlock? = nil) {