Switch to using the new DatabaseError.

This commit is contained in:
Brent Simmons 2019-12-15 22:37:45 -08:00
parent b1552af911
commit e0f61c362b
3 changed files with 27 additions and 42 deletions

View File

@ -17,17 +17,11 @@ import Articles
// Main thread only.
/// Sync methods may throw this error. Async methods use a result type which will include
/// this error when the database is suspended and therefore not available.
public enum ArticlesDatabaseError: Error {
case databaseIsSuspended
}
public typealias UnreadCountDictionary = [String: Int] // webFeedID: unreadCount
public typealias UnreadCountDictionaryCompletionResult = Result<UnreadCountDictionary, ArticlesDatabaseError>
public typealias UnreadCountDictionaryCompletionResult = Result<UnreadCountDictionary,DatabaseError>
public typealias UnreadCountDictionaryCompletionBlock = (UnreadCountDictionaryCompletionResult) -> Void
public typealias SingleUnreadCountResult = Result<Int, ArticlesDatabaseError>
public typealias SingleUnreadCountResult = Result<Int, DatabaseError>
public typealias SingleUnreadCountCompletionBlock = (SingleUnreadCountResult) -> Void
public struct NewAndUpdatedArticles {
@ -35,18 +29,16 @@ public struct NewAndUpdatedArticles {
let updatedArticles: Set<Article>?
}
public typealias UpdateArticlesResult = Result<NewAndUpdatedArticles, ArticlesDatabaseError>
public typealias UpdateArticlesResult = Result<NewAndUpdatedArticles, DatabaseError>
public typealias UpdateArticlesCompletionBlock = (UpdateArticlesResult) -> Void
public typealias ArticleSetResult = Result<Set<Article>, ArticlesDatabaseError>
public typealias ArticleSetResult = Result<Set<Article>, DatabaseError>
public typealias ArticleSetResultBlock = (ArticleSetResult) -> Void
public typealias DatabaseCompletionBlock = (ArticlesDatabaseError?) -> Void
public typealias ArticleIDsResult = Result<Set<String>, ArticlesDatabaseError>
public typealias ArticleIDsResult = Result<Set<String>, DatabaseError>
public typealias ArticleIDsCompletionBlock = (ArticleIDsResult) -> Void
public typealias ArticleStatusesResult = Result<Set<ArticleStatus>, ArticlesDatabaseError>
public typealias ArticleStatusesResult = Result<Set<ArticleStatus>, DatabaseError>
public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void
public final class ArticlesDatabase {
@ -231,13 +223,6 @@ public final class ArticlesDatabase {
}
}
func databaseError(with databaseQueueError: DatabaseQueueError) -> ArticlesDatabaseError {
switch databaseQueueError {
case .isSuspended:
return .databaseIsSuspended
}
}
// MARK: - Private
private extension ArticlesDatabase {

View File

@ -103,13 +103,13 @@ final class ArticlesTable: DatabaseTable {
func fetchArticlesMatching(_ searchString: String) throws -> Set<Article> {
var articles: Set<Article> = Set<Article>()
var error: ArticlesDatabaseError? = nil
var error: DatabaseError? = nil
queue.runInDatabaseSync { (databaseResult) in
switch databaseResult {
case .success(let database):
articles = self.fetchArticlesMatching(searchString, database)
case .failure(let databaseQueueError):
error = databaseError(with: databaseQueueError)
case .failure(let databaseError):
error = databaseError
}
}
if let error = error {
@ -190,7 +190,7 @@ final class ArticlesTable: DatabaseTable {
self.queue.runInTransaction { (databaseResult) in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseError(with: databaseResult.error!)))
completion(.failure(databaseResult.error!))
}
return
}
@ -233,7 +233,7 @@ final class ArticlesTable: DatabaseTable {
queue.runInTransaction { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion?(databaseError(with: databaseResult.error!))
completion?(databaseResult.error!)
}
return
}
@ -253,7 +253,7 @@ final class ArticlesTable: DatabaseTable {
queue.runInTransaction { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseError(with: databaseResult.error!)))
completion(.failure(databaseResult.error!))
}
return
}
@ -283,7 +283,7 @@ final class ArticlesTable: DatabaseTable {
queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseError(with: databaseResult.error!)))
completion(.failure(databaseResult.error!))
}
return
}
@ -309,7 +309,7 @@ final class ArticlesTable: DatabaseTable {
queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseError(with: databaseResult.error!)))
completion(.failure(databaseResult.error!))
}
return
}
@ -337,7 +337,7 @@ final class ArticlesTable: DatabaseTable {
queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseError(with: databaseResult.error!)))
completion(.failure(databaseResult.error!))
}
return
}
@ -374,7 +374,7 @@ final class ArticlesTable: DatabaseTable {
queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseError(with: databaseResult.error!)))
completion(.failure(databaseResult.error!))
}
return
}
@ -411,13 +411,13 @@ final class ArticlesTable: DatabaseTable {
func mark(_ articles: Set<Article>, _ statusKey: ArticleStatus.Key, _ flag: Bool) throws -> Set<ArticleStatus>? {
var statuses: Set<ArticleStatus>?
var error: ArticlesDatabaseError?
var error: DatabaseError?
self.queue.runInTransactionSync { databaseResult in
switch databaseResult {
case .success(let database):
statuses = self.statusesTable.mark(articles.statuses(), statusKey, flag, database)
case .failure(let databaseQueueError):
error = databaseError(with: databaseQueueError)
case .failure(let databaseError):
error = databaseError
}
}
@ -496,13 +496,13 @@ private extension ArticlesTable {
private func fetchArticles(_ fetchMethod: @escaping ArticlesFetchMethod) throws -> Set<Article> {
var articles = Set<Article>()
var error: ArticlesDatabaseError? = nil
var error: DatabaseError? = nil
queue.runInDatabaseSync { databaseResult in
switch databaseResult {
case .success(let database):
articles = fetchMethod(database)
case .failure(let databaseQueueError):
error = databaseError(with: databaseQueueError)
case .failure(let databaseError):
error = databaseError
}
}
if let error = error {
@ -515,7 +515,7 @@ private extension ArticlesTable {
queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseError(with: databaseResult.error!)))
completion(.failure(databaseResult.error!))
}
return
}
@ -684,7 +684,7 @@ private extension ArticlesTable {
queue.runInDatabase { databaseResult in
guard let database = databaseResult.database else {
DispatchQueue.main.async {
completion(.failure(databaseError(with: databaseResult.error!)))
completion(.failure(databaseResult.error!))
}
return
}

View File

@ -100,7 +100,7 @@ final class StatusesTable: DatabaseTable {
}
func fetchArticleIDs(_ sql: String) throws -> Set<String> {
var error: ArticlesDatabaseError?
var error: DatabaseError?
var articleIDs = Set<String>()
queue.runInDatabaseSync { databaseResult in
switch databaseResult {
@ -108,8 +108,8 @@ final class StatusesTable: DatabaseTable {
if let resultSet = database.executeQuery(sql, withArgumentsIn: nil) {
articleIDs = resultSet.mapToSet(self.articleIDWithRow)
}
case .failure(let databaseQueueError):
error = databaseError(with: databaseQueueError)
case .failure(let databaseError):
error = databaseError
}
}