Make SyncDatabase selectPendingCount async.

This commit is contained in:
Brent Simmons 2019-12-16 10:50:13 -08:00
parent 894d3909a3
commit 7a134740ec
6 changed files with 37 additions and 29 deletions

View File

@ -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 }
}
}

View File

@ -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) {

View File

@ -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)
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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) {