Finish making Account and AccountManager compatible with DatabaseQueue changes.

This commit is contained in:
Brent Simmons 2019-12-16 17:03:41 -08:00
parent 9516392c61
commit 51393967e2
2 changed files with 44 additions and 21 deletions

View File

@ -624,7 +624,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
public func fetchArticles(_ fetchType: FetchType) throws -> Set<Article> {
switch fetchType {
case .starred:
return fetchStarredArticles()
return try fetchStarredArticles()
case .unread:
return try fetchUnreadArticles()
case .today:
@ -784,7 +784,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
/// Update statuses specified by articleIDs  set a key and value.
/// This updates the database, and sends a .StatusesDidChange notification.
/// Any statuses that dont exist will be automatically created.
func mark(articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool, completion: @escaping DatabaseCompletionBlock? = nil) {
func mark(articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool, completion: DatabaseCompletionBlock? = nil) {
// TODO
}
@ -853,7 +853,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
public func debugRunSearch() {
#if DEBUG
let t1 = Date()
let articles = fetchArticlesMatching("Brent NetNewsWire")
let articles = try! fetchArticlesMatching("Brent NetNewsWire")
let t2 = Date()
print(t2.timeIntervalSince(t1))
print(articles.count)
@ -940,8 +940,8 @@ extension Account: WebFeedMetadataDelegate {
private extension Account {
func fetchStarredArticles() -> Set<Article> {
return database.fetchStarredArticles(flattenedWebFeeds().webFeedIDs())
func fetchStarredArticles() throws -> Set<Article> {
return try database.fetchStarredArticles(flattenedWebFeeds().webFeedIDs())
}
func fetchStarredArticlesAsync(_ completion: @escaping ArticleSetResultBlock) {
@ -987,9 +987,14 @@ private extension Account {
}
func fetchArticlesAsync(webFeed: WebFeed, _ completion: @escaping ArticleSetResultBlock) {
database.fetchArticlesAsync(webFeed.webFeedID) { [weak self] (articles) in
self?.validateUnreadCount(webFeed, articles)
completion(articles)
database.fetchArticlesAsync(webFeed.webFeedID) { [weak self] articleSetResult in
switch articleSetResult {
case .success(let articles):
self?.validateUnreadCount(webFeed, articles)
completion(.success(articles))
case .failure(let databaseError):
completion(.failure(databaseError))
}
}
}
@ -1040,9 +1045,14 @@ private extension Account {
func fetchArticlesAsync(forContainer container: Container, _ completion: @escaping ArticleSetResultBlock) {
let webFeeds = container.flattenedWebFeeds()
database.fetchArticlesAsync(webFeeds.webFeedIDs()) { [weak self] (articles) in
self?.validateUnreadCountsAfterFetchingUnreadArticles(webFeeds, articles)
completion(articles)
database.fetchArticlesAsync(webFeeds.webFeedIDs()) { [weak self] (articleSetResult) in
switch articleSetResult {
case .success(let articles):
self?.validateUnreadCountsAfterFetchingUnreadArticles(webFeeds, articles)
completion(.success(articles))
case .failure(let databaseError):
completion(.failure(databaseError))
}
}
}
@ -1055,9 +1065,14 @@ private extension Account {
func fetchUnreadArticlesAsync(forContainer container: Container, _ completion: @escaping ArticleSetResultBlock) {
let webFeeds = container.flattenedWebFeeds()
database.fetchUnreadArticlesAsync(webFeeds.webFeedIDs()) { [weak self] (articles) in
self?.validateUnreadCountsAfterFetchingUnreadArticles(webFeeds, articles)
completion(articles)
database.fetchUnreadArticlesAsync(webFeeds.webFeedIDs()) { [weak self] (articleSetResult) in
switch articleSetResult {
case .success(let articles):
self?.validateUnreadCountsAfterFetchingUnreadArticles(webFeeds, articles)
completion(.success(articles))
case .failure(let databaseError):
completion(.failure(databaseError))
}
}
}

View File

@ -9,6 +9,7 @@
import Foundation
import RSCore
import Articles
import ArticlesDatabase
// Main thread only.
@ -255,17 +256,17 @@ public final class AccountManager: UnreadCountProvider {
// These fetch articles from active accounts and return a merged Set<Article>.
public func fetchArticles(_ fetchType: FetchType) -> Set<Article> {
public func fetchArticles(_ fetchType: FetchType) throws -> Set<Article> {
precondition(Thread.isMainThread)
var articles = Set<Article>()
for account in activeAccounts {
articles.formUnion(account.fetchArticles(fetchType))
articles.formUnion(try account.fetchArticles(fetchType))
}
return articles
}
public func fetchArticlesAsync(_ fetchType: FetchType, _ completion: @escaping ArticleSetBlock) {
public func fetchArticlesAsync(_ fetchType: FetchType, _ completion: @escaping ArticleSetResultBlock) {
precondition(Thread.isMainThread)
var allFetchedArticles = Set<Article>()
@ -273,11 +274,18 @@ public final class AccountManager: UnreadCountProvider {
var accountsReporting = 0
for account in activeAccounts {
account.fetchArticlesAsync(fetchType) { (articles) in
allFetchedArticles.formUnion(articles)
account.fetchArticlesAsync(fetchType) { (articleSetResult) in
accountsReporting += 1
if accountsReporting == numberOfAccounts {
completion(allFetchedArticles)
switch articleSetResult {
case .success(let articles):
allFetchedArticles.formUnion(articles)
if accountsReporting == numberOfAccounts {
completion(.success(allFetchedArticles))
}
case .failure(let databaseError):
completion(.failure(databaseError))
return
}
}
}