Make selectForProcessing async.

This commit is contained in:
Brent Simmons 2019-12-16 11:08:47 -08:00
parent 746708dd47
commit 7b7346d010
2 changed files with 17 additions and 11 deletions

View File

@ -10,6 +10,9 @@ import Foundation
import RSCore
import RSDatabase
public typealias SyncStatusesResult = Result<Array<SyncStatus>, DatabaseError>
public typealias SyncStatusesCompletionBlock = (SyncStatusesResult) -> Void
public struct SyncDatabase {
private let syncStatusTable: SyncStatusTable
@ -30,8 +33,8 @@ public struct SyncDatabase {
syncStatusTable.insertStatuses(statuses, completion: completion)
}
public func selectForProcessing() throws -> [SyncStatus] {
return try syncStatusTable.selectForProcessing()
public func selectForProcessing(completion: @escaping SyncStatusesCompletionBlock) {
return syncStatusTable.selectForProcessing(completion)
}
public func selectPendingCount(completion: @escaping DatabaseIntCompletionBlock) {

View File

@ -20,11 +20,10 @@ struct SyncStatusTable: DatabaseTable {
self.queue = queue
}
func selectForProcessing() throws -> [SyncStatus] {
var statuses: Set<SyncStatus>? = nil
var error: DatabaseError?
queue.runInTransactionSync { databaseResult in
func selectForProcessing(_ completion: @escaping SyncStatusesCompletionBlock) {
queue.runInTransaction { databaseResult in
var statuses = Set<SyncStatus>()
var error: DatabaseError?
func makeDatabaseCall(_ database: FMDatabase) {
let updateSQL = "update syncStatus set selected = true"
@ -42,12 +41,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(Array(statuses)))
}
}
}
return statuses != nil ? Array(statuses!) : [SyncStatus]()
}
func selectPendingCount(_ completion: @escaping DatabaseIntCompletionBlock) {