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. // 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 UnreadCountDictionary = [String: Int] // webFeedID: unreadCount
public typealias UnreadCountDictionaryCompletionResult = Result<UnreadCountDictionary, ArticlesDatabaseError> public typealias UnreadCountDictionaryCompletionResult = Result<UnreadCountDictionary,DatabaseError>
public typealias UnreadCountDictionaryCompletionBlock = (UnreadCountDictionaryCompletionResult) -> Void public typealias UnreadCountDictionaryCompletionBlock = (UnreadCountDictionaryCompletionResult) -> Void
public typealias SingleUnreadCountResult = Result<Int, ArticlesDatabaseError> public typealias SingleUnreadCountResult = Result<Int, DatabaseError>
public typealias SingleUnreadCountCompletionBlock = (SingleUnreadCountResult) -> Void public typealias SingleUnreadCountCompletionBlock = (SingleUnreadCountResult) -> Void
public struct NewAndUpdatedArticles { public struct NewAndUpdatedArticles {
@ -35,18 +29,16 @@ public struct NewAndUpdatedArticles {
let updatedArticles: Set<Article>? let updatedArticles: Set<Article>?
} }
public typealias UpdateArticlesResult = Result<NewAndUpdatedArticles, ArticlesDatabaseError> public typealias UpdateArticlesResult = Result<NewAndUpdatedArticles, DatabaseError>
public typealias UpdateArticlesCompletionBlock = (UpdateArticlesResult) -> Void 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 ArticleSetResultBlock = (ArticleSetResult) -> Void
public typealias DatabaseCompletionBlock = (ArticlesDatabaseError?) -> Void public typealias ArticleIDsResult = Result<Set<String>, DatabaseError>
public typealias ArticleIDsResult = Result<Set<String>, ArticlesDatabaseError>
public typealias ArticleIDsCompletionBlock = (ArticleIDsResult) -> Void 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 typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void
public final class ArticlesDatabase { 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 // MARK: - Private
private extension ArticlesDatabase { private extension ArticlesDatabase {

View File

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

View File

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