Continue post-DatabaseResult simplification.

This commit is contained in:
Brent Simmons committed 2026-06-10 16:59:28 -07:00
1 parent fcaa1cc03c
commit 1066ca2720
25 files changed
+215 -259

No files matched your search

+2 -1
View File
@@ -1155,7 +1155,8 @@ private extension AppDelegate {
return
}
guard let singleArticleSet = try? account.fetchArticles(.articleIDs([articleID])) else {
let singleArticleSet = account.fetchArticles(.articleIDs([articleID]))
guard singleArticleSet.count > 0 else {
assertionFailure("Expected article with \(articleID)")
Self.logger.error("No article with articleID found \(articleID) from status notification")
return
@@ -346,9 +346,8 @@ private extension SidebarViewController {
var articles = Set<Article>()
for object in objects {
if let articleFetcher = object as? ArticleFetcher {
if let unreadArticles = try? articleFetcher.fetchUnreadArticles() {
articles.formUnion(unreadArticles)
}
let unreadArticles = articleFetcher.fetchUnreadArticles()
articles.formUnion(unreadArticles)
}
}
return articles
@@ -299,7 +299,7 @@ extension Notification.Name {
guard outlineView.clickedRow == outlineView.selectedRow else {
return
}
if AppDefaults.shared.feedDoubleClickMarkAsRead, let articles = try? singleSelectedFeed?.fetchUnreadArticles() {
if AppDefaults.shared.feedDoubleClickMarkAsRead, let articles = singleSelectedFeed?.fetchUnreadArticles() {
if let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: Array(articles), markingRead: true, undoManager: undoManager) {
runCommand(markReadCommand)
}
@@ -77,7 +77,8 @@ extension TimelineViewController {
return
}
guard let unreadArticles = try? feed.fetchUnreadArticles(), !unreadArticles.isEmpty else {
let unreadArticles = feed.fetchUnreadArticles()
guard !unreadArticles.isEmpty else {
return
}
guard let undoManager, let markReadCommand = MarkStatusCommand(
@@ -1229,13 +1229,11 @@ private extension TimelineViewController {
var fetchedArticles = Set<Article>()
for fetchers in fetchers {
if (fetchers as? SidebarItem)?.readFiltered(readFilterEnabledTable: readFilterEnabledTable) ?? true {
if let articles = try? fetchers.fetchUnreadArticles() {
fetchedArticles.formUnion(articles)
}
let articles = fetchers.fetchUnreadArticles()
fetchedArticles.formUnion(articles)
} else {
if let articles = try? fetchers.fetchArticles() {
fetchedArticles.formUnion(articles)
}
let articles = fetchers.fetchArticles()
fetchedArticles.formUnion(articles)
}
}
return fetchedArticles
+2 -2
View File
@@ -193,7 +193,7 @@ import Articles
@objc(articles)
var articles: NSArray {
let feedArticles = (try? feed.fetchArticles()) ?? Set<Article>()
let feedArticles = feed.fetchArticles()
// the articles are a set, use the sorting algorithm from the viewer
let sortedArticles = feedArticles.sorted(by: {
return $0.logicalDatePublished > $1.logicalDatePublished
@@ -203,7 +203,7 @@ import Articles
@objc(valueInArticlesWithUniqueID:)
func valueInArticles(withUniqueID id: String) -> ScriptableArticle? {
let articles = (try? feed.fetchArticles()) ?? Set<Article>()
let articles = feed.fetchArticles()
guard let article = articles.first(where: {$0.uniqueID == id}) else {
return nil
}
+3 -3
View File
@@ -106,7 +106,7 @@ import RSCore
var articles: NSArray {
let feeds = Array(folder.topLevelFeeds)
let allArticles = feeds.flatMap { feed in
(try? feed.fetchArticles()) ?? Set<Article>()
feed.fetchArticles()
}
// Sort articles by logical date published like Feed does
let sortedArticles = allArticles.sorted(by: {
@@ -119,7 +119,7 @@ import RSCore
func countOfArticles() -> Int {
let feeds = Array(folder.topLevelFeeds)
return feeds.reduce(0) { count, feed in
let feedArticles = (try? feed.fetchArticles()) ?? Set<Article>()
let feedArticles = feed.fetchArticles()
return count + feedArticles.count
}
}
@@ -128,7 +128,7 @@ import RSCore
func objectInArticlesAtIndex(_ index: Int) -> ScriptableArticle? {
let feeds = Array(folder.topLevelFeeds)
let allArticles = feeds.flatMap { feed in
(try? feed.fetchArticles()) ?? Set<Article>()
feed.fetchArticles()
}
let sortedArticles = allArticles.sorted(by: {
return $0.logicalDatePublished > $1.logicalDatePublished
+36 -36
View File
@@ -812,7 +812,7 @@ public enum FetchType {
// MARK: - Fetching Articles
public func fetchArticles(_ fetchType: FetchType) throws -> Set<Article> {
public func fetchArticles(_ fetchType: FetchType) -> Set<Article> {
switch fetchType {
case .starred(let limit):
return _fetchStarredArticles(limit: limit)
@@ -831,34 +831,34 @@ public enum FetchType {
case .articleIDs(let articleIDs):
return _fetchArticles(articleIDs: articleIDs)
case .search(let searchString):
return try _fetchArticlesMatching(searchString: searchString)
return _fetchArticlesMatching(searchString: searchString)
case .searchWithArticleIDs(let searchString, let articleIDs):
return try _fetchArticlesMatchingWithArticleIDs(searchString: searchString, articleIDs: articleIDs)
return _fetchArticlesMatchingWithArticleIDs(searchString: searchString, articleIDs: articleIDs)
}
}
public func fetchArticlesAsync(_ fetchType: FetchType) async throws -> Set<Article> {
public func fetchArticlesAsync(_ fetchType: FetchType) async -> Set<Article> {
switch fetchType {
case .starred(let limit):
return try await _fetchStarredArticlesAsync(limit: limit)
return await _fetchStarredArticlesAsync(limit: limit)
case .unread(let limit):
return try await _fetchUnreadArticlesAsync(limit: limit)
return await _fetchUnreadArticlesAsync(limit: limit)
case .today(let limit):
return try await _fetchTodayArticlesAsync(limit: limit)
return await _fetchTodayArticlesAsync(limit: limit)
case .folder(let folder, let readFilter):
if readFilter {
return try await _fetchUnreadArticlesAsync(container: folder)
return await _fetchUnreadArticlesAsync(container: folder)
} else {
return try await _fetchArticlesAsync(container: folder)
return await _fetchArticlesAsync(container: folder)
}
case .feed(let feed):
return try await _fetchArticlesAsync(feed: feed)
return await _fetchArticlesAsync(feed: feed)
case .articleIDs(let articleIDs):
return try await _fetchArticlesAsync(articleIDs: articleIDs)
return await _fetchArticlesAsync(articleIDs: articleIDs)
case .search(let searchString):
return try await _fetchArticlesMatchingAsync(searchString: searchString)
return await _fetchArticlesMatchingAsync(searchString: searchString)
case .searchWithArticleIDs(let searchString, let articleIDs):
return try await _fetchArticlesMatchingWithArticleIDsAsync(searchString: searchString, articleIDs: articleIDs)
return await _fetchArticlesMatchingWithArticleIDsAsync(searchString: searchString, articleIDs: articleIDs)
}
}
@@ -1133,7 +1133,7 @@ public enum FetchType {
public func debugRunSearch() {
#if DEBUG
let t1 = Date()
let articles = try! _fetchArticlesMatching(searchString: "Brent NetNewsWire")
let articles = _fetchArticlesMatching(searchString: "Brent NetNewsWire")
let t2 = Date()
print(t2.timeIntervalSince(t1))
print(articles.count)
@@ -1207,8 +1207,8 @@ private extension Account {
database.fetchStarredArticles(feedIDs: flattenedFeedsIDs, limit: limit)
}
func _fetchStarredArticlesAsync(limit: Int? = nil) async throws -> Set<Article> {
try await database.fetchedStarredArticlesAsync(feedIDs: flattenedFeedsIDs, limit: limit)
func _fetchStarredArticlesAsync(limit: Int? = nil) async -> Set<Article> {
await database.fetchedStarredArticlesAsync(feedIDs: flattenedFeedsIDs, limit: limit)
}
// MARK: - Account Unread Articles
@@ -1217,8 +1217,8 @@ private extension Account {
_fetchUnreadArticles(container: self, limit: limit)
}
func _fetchUnreadArticlesAsync(limit: Int? = nil) async throws -> Set<Article> {
try await _fetchUnreadArticlesAsync(container: self, limit: limit)
func _fetchUnreadArticlesAsync(limit: Int? = nil) async -> Set<Article> {
await _fetchUnreadArticlesAsync(container: self, limit: limit)
}
// MARK: - Today Articles
@@ -1227,8 +1227,8 @@ private extension Account {
database.fetchTodayArticles(feedIDs: flattenedFeedsIDs, limit: limit)
}
func _fetchTodayArticlesAsync(limit: Int? = nil) async throws -> Set<Article> {
try await database.fetchTodayArticlesAsync(feedIDs: flattenedFeedsIDs, limit: limit)
func _fetchTodayArticlesAsync(limit: Int? = nil) async -> Set<Article> {
await database.fetchTodayArticlesAsync(feedIDs: flattenedFeedsIDs, limit: limit)
}
// MARK: - Container Articles
@@ -1240,9 +1240,9 @@ private extension Account {
return articles
}
func _fetchArticlesAsync(container: Container) async throws -> Set<Article> {
func _fetchArticlesAsync(container: Container) async -> Set<Article> {
let feeds = container.flattenedFeeds()
let articles = try await database.fetchArticlesAsync(feedIDs: feeds.feedIDs())
let articles = await database.fetchArticlesAsync(feedIDs: feeds.feedIDs())
validateUnreadCountsAfterFetchingUnreadArticles(feeds: feeds, articles: articles)
return articles
}
@@ -1260,9 +1260,9 @@ private extension Account {
return articles
}
func _fetchUnreadArticlesAsync(container: Container, limit: Int? = nil) async throws -> Set<Article> {
func _fetchUnreadArticlesAsync(container: Container, limit: Int? = nil) async -> Set<Article> {
let feeds = container.flattenedFeeds()
let articles = try await database.fetchUnreadArticlesAsync(feedIDs: feeds.feedIDs(), limit: limit)
let articles = await database.fetchUnreadArticlesAsync(feedIDs: feeds.feedIDs(), limit: limit)
// We don't validate limit queries because they, by definition, won't correctly match the
// complete unread state for the given container.
@@ -1281,8 +1281,8 @@ private extension Account {
return articles
}
func _fetchArticlesAsync(feed: Feed) async throws -> Set<Article> {
let articles = try await database.fetchArticlesAsync(feedID: feed.feedID)
func _fetchArticlesAsync(feed: Feed) async -> Set<Article> {
let articles = await database.fetchArticlesAsync(feedID: feed.feedID)
validateUnreadCount(feed: feed, articles: articles)
return articles
}
@@ -1299,26 +1299,26 @@ private extension Account {
database.fetchArticles(articleIDs: articleIDs)
}
func _fetchArticlesAsync(articleIDs: Set<String>) async throws -> Set<Article> {
try await database.fetchArticlesAsync(articleIDs: articleIDs)
func _fetchArticlesAsync(articleIDs: Set<String>) async -> Set<Article> {
await database.fetchArticlesAsync(articleIDs: articleIDs)
}
// MARK: - Search Articles
func _fetchArticlesMatching(searchString: String) throws -> Set<Article> {
try database.fetchArticlesMatching(searchString: searchString, feedIDs: flattenedFeedsIDs)
func _fetchArticlesMatching(searchString: String) -> Set<Article> {
database.fetchArticlesMatching(searchString: searchString, feedIDs: flattenedFeedsIDs)
}
func _fetchArticlesMatchingAsync(searchString: String) async throws -> Set<Article> {
try await database.fetchArticlesMatchingAsync(searchString: searchString, feedIDs: flattenedFeedsIDs)
func _fetchArticlesMatchingAsync(searchString: String) async -> Set<Article> {
await database.fetchArticlesMatchingAsync(searchString: searchString, feedIDs: flattenedFeedsIDs)
}
func _fetchArticlesMatchingWithArticleIDs(searchString: String, articleIDs: Set<String>) throws -> Set<Article> {
try database.fetchArticlesMatchingWithArticleIDs(searchString: searchString, articleIDs: articleIDs)
func _fetchArticlesMatchingWithArticleIDs(searchString: String, articleIDs: Set<String>) -> Set<Article> {
database.fetchArticlesMatchingWithArticleIDs(searchString: searchString, articleIDs: articleIDs)
}
func _fetchArticlesMatchingWithArticleIDsAsync(searchString: String, articleIDs: Set<String>) async throws -> Set<Article> {
try await database.fetchArticlesMatchingWithArticleIDsAsync(searchString: searchString, articleIDs: articleIDs)
func _fetchArticlesMatchingWithArticleIDsAsync(searchString: String, articleIDs: Set<String>) async -> Set<Article> {
await database.fetchArticlesMatchingWithArticleIDsAsync(searchString: searchString, articleIDs: articleIDs)
}
// MARK: - Unread Counts
@@ -392,17 +392,17 @@ import ActivityLog
// These fetch articles from active accounts and return a merged Set<Article>.
public func fetchArticles(_ fetchType: FetchType) throws -> Set<Article> {
public func fetchArticles(_ fetchType: FetchType) -> Set<Article> {
precondition(Thread.isMainThread)
var articles = Set<Article>()
for account in activeAccounts {
articles.formUnion(try account.fetchArticles(fetchType))
articles.formUnion(account.fetchArticles(fetchType))
}
return articles
}
public func fetchArticlesAsync(_ fetchType: FetchType) async throws -> Set<Article> {
public func fetchArticlesAsync(_ fetchType: FetchType) async -> Set<Article> {
precondition(Thread.isMainThread)
guard activeAccounts.count > 0 else {
@@ -411,7 +411,7 @@ import ActivityLog
var allFetchedArticles = Set<Article>()
for account in activeAccounts {
let articles = try await account.fetchArticlesAsync(fetchType)
let articles = await account.fetchArticlesAsync(fetchType)
allFetchedArticles.formUnion(articles)
}
@@ -426,17 +426,13 @@ import ActivityLog
return nil
}
do {
let articles = try account.fetchArticles(.articleIDs(Set([articleID])))
return articles.first
} catch {
return nil
}
let articles = account.fetchArticles(.articleIDs(Set([articleID])))
return articles.first
}
// MARK: - Fetching Article Counts
public func fetchCountForStarredArticles() throws -> Int {
public func fetchCountForStarredArticles() -> Int {
precondition(Thread.isMainThread)
var count = 0
for account in activeAccounts {
@@ -11,72 +11,72 @@ import Articles
import ArticlesDatabase
@MainActor public protocol ArticleFetcher {
func fetchArticles() throws -> Set<Article>
func fetchArticlesAsync() async throws -> Set<Article>
func fetchUnreadArticles() throws -> Set<Article>
func fetchUnreadArticlesAsync() async throws -> Set<Article>
func fetchArticles() -> Set<Article>
func fetchArticlesAsync() async -> Set<Article>
func fetchUnreadArticles() -> Set<Article>
func fetchUnreadArticlesAsync() async -> Set<Article>
}
extension Feed: ArticleFetcher {
public func fetchArticles() throws -> Set<Article> {
return try account?.fetchArticles(.feed(self)) ?? Set<Article>()
public func fetchArticles() -> Set<Article> {
account?.fetchArticles(.feed(self)) ?? Set<Article>()
}
public func fetchArticlesAsync() async throws -> Set<Article> {
public func fetchArticlesAsync() async -> Set<Article> {
guard let account else {
assertionFailure("Expected feed.account, but got nil.")
return Set<Article>()
}
return try await account.fetchArticlesAsync(.feed(self))
return await account.fetchArticlesAsync(.feed(self))
}
public func fetchUnreadArticles() throws -> Set<Article> {
return try fetchArticles().unreadArticles()
public func fetchUnreadArticles() -> Set<Article> {
fetchArticles().unreadArticles()
}
public func fetchUnreadArticlesAsync() async throws -> Set<Article> {
public func fetchUnreadArticlesAsync() async -> Set<Article> {
guard let account else {
assertionFailure("Expected feed.account, but got nil.")
return Set<Article>()
}
// TODO: fetch only unread articles rather than filtering.
let articles = try await account.fetchArticlesAsync(.feed(self))
let articles = await account.fetchArticlesAsync(.feed(self))
return articles.unreadArticles()
}
}
extension Folder: ArticleFetcher {
public func fetchArticles() throws -> Set<Article> {
public func fetchArticles() -> Set<Article> {
guard let account else {
assertionFailure("Expected folder.account, but got nil.")
return Set<Article>()
}
return try account.fetchArticles(.folder(self, false))
return account.fetchArticles(.folder(self, false))
}
public func fetchArticlesAsync() async throws -> Set<Article> {
public func fetchArticlesAsync() async -> Set<Article> {
guard let account else {
assertionFailure("Expected folder.account, but got nil.")
return Set<Article>()
}
return try await account.fetchArticlesAsync(.folder(self, false))
return await account.fetchArticlesAsync(.folder(self, false))
}
public func fetchUnreadArticles() throws -> Set<Article> {
public func fetchUnreadArticles() -> Set<Article> {
guard let account else {
assertionFailure("Expected folder.account, but got nil.")
return Set<Article>()
}
return try account.fetchArticles(.folder(self, true))
return account.fetchArticles(.folder(self, true))
}
public func fetchUnreadArticlesAsync() async throws -> Set<Article> {
public func fetchUnreadArticlesAsync() async -> Set<Article> {
guard let account else {
assertionFailure("Expected folder.account, but got nil.")
return Set<Article>()
}
return try await account.fetchArticlesAsync(.folder(self, true))
return await account.fetchArticlesAsync(.folder(self, true))
}
}
@@ -939,12 +939,13 @@ private extension CloudKitAccountDelegate {
Self.logger.debug("CloudKitAccountDelegate: \(#function, privacy: .public)")
Task {
do {
let articles = try await account.fetchArticlesAsync(.feed(feed))
let articles = await account.fetchArticlesAsync(.feed(feed))
await storeArticleChanges(new: articles, updated: Set<Article>(), deleted: Set<Article>())
syncProgress.completeTask()
_ = try await sendArticleStatus(account: account, showProgress: true)
do {
try await articlesZone.fetchChangesInZone()
} catch {
@@ -99,15 +99,7 @@ final class CloudKitSendStatusOperation: MainThreadOperation, @unchecked Sendabl
}
let articleIDs = syncStatuses.map({ $0.articleID })
let articles: Set<Article>
do {
articles = try await account.fetchArticlesAsync(.articleIDs(Set(articleIDs)))
} catch {
await syncDatabase.resetSelectedForProcessing(Set(syncStatuses.map({ $0.articleID })))
Self.logger.error("iCloud: Send article status fetch articles error: \(error.localizedDescription)")
throw error
}
let articles = await account.fetchArticlesAsync(.articleIDs(Set(articleIDs)))
let syncStatusesDict = Dictionary(grouping: syncStatuses, by: { $0.articleID })
let articlesDict = articles.reduce(into: [String: Article]()) { result, article in
@@ -20,19 +20,19 @@ public struct SingleArticleFetcher: ArticleFetcher {
self.articleID = articleID
}
public func fetchArticles() throws -> Set<Article> {
try account.fetchArticles(.articleIDs(Set([articleID])))
public func fetchArticles() -> Set<Article> {
account.fetchArticles(.articleIDs(Set([articleID])))
}
public func fetchArticlesAsync() async throws -> Set<Article> {
try await account.fetchArticlesAsync(.articleIDs(Set([articleID])))
public func fetchArticlesAsync() async -> Set<Article> {
await account.fetchArticlesAsync(.articleIDs(Set([articleID])))
}
public func fetchUnreadArticles() throws -> Set<Article> {
try account.fetchArticles(.articleIDs(Set([articleID])))
public func fetchUnreadArticles() -> Set<Article> {
account.fetchArticles(.articleIDs(Set([articleID])))
}
public func fetchUnreadArticlesAsync() async throws -> Set<Article> {
try await account.fetchArticlesAsync(.articleIDs(Set([articleID])))
public func fetchUnreadArticlesAsync() async -> Set<Article> {
await account.fetchArticlesAsync(.articleIDs(Set([articleID])))
}
}
@@ -153,14 +153,14 @@ public struct ArticleCounts: Sendable {
}
}
public func fetchArticlesMatching(searchString: String, feedIDs: Set<String>) throws -> Set<Article> {
public func fetchArticlesMatching(searchString: String, feedIDs: Set<String>) -> Set<Article> {
Self.logger.debug("ArticlesDatabase: \(#function, privacy: .public) \(self.accountID, privacy: .public)")
return try articlesTable.fetchArticlesMatching(searchString, feedIDs)
return articlesTable.fetchArticlesMatching(searchString, feedIDs)
}
public func fetchArticlesMatchingWithArticleIDs(searchString: String, articleIDs: Set<String>) throws -> Set<Article> {
public func fetchArticlesMatchingWithArticleIDs(searchString: String, articleIDs: Set<String>) -> Set<Article> {
Self.logger.debug("ArticlesDatabase: \(#function, privacy: .public) \(self.accountID, privacy: .public)")
return try articlesTable.fetchArticlesMatchingWithArticleIDs(searchString, articleIDs)
return articlesTable.fetchArticlesMatchingWithArticleIDs(searchString, articleIDs)
}
/// Returns a dictionary of feedID → latest article date for all feeds with articles.
@@ -175,66 +175,66 @@ public struct ArticleCounts: Sendable {
// MARK: - Fetching Articles Async
public func fetchArticlesAsync(feedID: String) async throws -> Set<Article> {
try await withCheckedThrowingContinuation { continuation in
_fetchArticlesAsync(feedID: feedID) { result in
continuation.resume(with: result)
public func fetchArticlesAsync(feedID: String) async -> Set<Article> {
await withCheckedContinuation { continuation in
_fetchArticlesAsync(feedID: feedID) { articles in
continuation.resume(returning: articles)
}
}
}
public func fetchArticlesAsync(feedIDs: Set<String>) async throws -> Set<Article> {
try await withCheckedThrowingContinuation { continuation in
_fetchArticlesAsync(feedIDs: feedIDs) { result in
continuation.resume(with: result)
public func fetchArticlesAsync(feedIDs: Set<String>) async -> Set<Article> {
await withCheckedContinuation { continuation in
_fetchArticlesAsync(feedIDs: feedIDs) { articles in
continuation.resume(returning: articles)
}
}
}
public func fetchArticlesAsync(articleIDs: Set<String>) async throws -> Set<Article> {
try await withCheckedThrowingContinuation { continuation in
_fetchArticlesAsync(articleIDs: articleIDs) { result in
continuation.resume(with: result)
public func fetchArticlesAsync(articleIDs: Set<String>) async -> Set<Article> {
await withCheckedContinuation { continuation in
_fetchArticlesAsync(articleIDs: articleIDs) { articles in
continuation.resume(returning: articles)
}
}
}
public func fetchUnreadArticlesAsync(feedIDs: Set<String>, limit: Int? = nil) async throws -> Set<Article> {
try await withCheckedThrowingContinuation { continuation in
_fetchUnreadArticlesAsync(feedIDs: feedIDs, limit: limit) { result in
continuation.resume(with: result)
public func fetchUnreadArticlesAsync(feedIDs: Set<String>, limit: Int? = nil) async -> Set<Article> {
await withCheckedContinuation { continuation in
_fetchUnreadArticlesAsync(feedIDs: feedIDs, limit: limit) { articles in
continuation.resume(returning: articles)
}
}
}
public func fetchTodayArticlesAsync(feedIDs: Set<String>, limit: Int? = nil) async throws -> Set<Article> {
try await withCheckedThrowingContinuation { continuation in
_fetchTodayArticlesAsync(feedIDs: feedIDs, limit: limit) { result in
continuation.resume(with: result)
public func fetchTodayArticlesAsync(feedIDs: Set<String>, limit: Int? = nil) async -> Set<Article> {
await withCheckedContinuation { continuation in
_fetchTodayArticlesAsync(feedIDs: feedIDs, limit: limit) { articles in
continuation.resume(returning: articles)
}
}
}
public func fetchedStarredArticlesAsync(feedIDs: Set<String>, limit: Int? = nil) async throws -> Set<Article> {
try await withCheckedThrowingContinuation { continuation in
_fetchedStarredArticlesAsync(feedIDs: feedIDs, limit: limit) { result in
continuation.resume(with: result)
public func fetchedStarredArticlesAsync(feedIDs: Set<String>, limit: Int? = nil) async -> Set<Article> {
await withCheckedContinuation { continuation in
_fetchedStarredArticlesAsync(feedIDs: feedIDs, limit: limit) { articles in
continuation.resume(returning: articles)
}
}
}
public func fetchArticlesMatchingAsync(searchString: String, feedIDs: Set<String>) async throws -> Set<Article> {
try await withCheckedThrowingContinuation { continuation in
_fetchArticlesMatchingAsync(searchString: searchString, feedIDs: feedIDs) { result in
continuation.resume(with: result)
public func fetchArticlesMatchingAsync(searchString: String, feedIDs: Set<String>) async -> Set<Article> {
await withCheckedContinuation { continuation in
_fetchArticlesMatchingAsync(searchString: searchString, feedIDs: feedIDs) { articles in
continuation.resume(returning: articles)
}
}
}
public func fetchArticlesMatchingWithArticleIDsAsync(searchString: String, articleIDs: Set<String>) async throws -> Set<Article> {
try await withCheckedThrowingContinuation { continuation in
_fetchArticlesMatchingWithArticleIDsAsync(searchString: searchString, articleIDs: articleIDs) { result in
continuation.resume(with: result)
public func fetchArticlesMatchingWithArticleIDsAsync(searchString: String, articleIDs: Set<String>) async -> Set<Article> {
await withCheckedContinuation { continuation in
_fetchArticlesMatchingWithArticleIDsAsync(searchString: searchString, articleIDs: articleIDs) { articles in
continuation.resume(returning: articles)
}
}
}
@@ -294,8 +294,8 @@ public struct ArticleCounts: Sendable {
/// Update articles and save new ones — for feed-based systems (local and iCloud).
public func updateAsync(parsedItems: Set<ParsedItem>, feedID: String, deleteOlder: Bool) async throws -> ArticleChanges {
try await withCheckedThrowingContinuation { continuation in
_update(parsedItems: parsedItems, feedID: feedID, deleteOlder: deleteOlder) { result in
continuation.resume(with: result)
_update(parsedItems: parsedItems, feedID: feedID, deleteOlder: deleteOlder) { articleChanges in
continuation.resume(returning: articleChanges)
}
}
}
@@ -303,8 +303,8 @@ public struct ArticleCounts: Sendable {
/// Update articles and save new ones — for sync systems (Feedbin, Feedly, etc.).
public func updateAsync(feedIDsAndItems: [String: Set<ParsedItem>], defaultRead: Bool) async throws -> ArticleChanges {
try await withCheckedThrowingContinuation { continuation in
_update(feedIDsAndItems: feedIDsAndItems, defaultRead: defaultRead) { result in
continuation.resume(with: result)
_update(feedIDsAndItems: feedIDsAndItems, defaultRead: defaultRead) { articleChanges in
continuation.resume(returning: articleChanges)
}
}
}
@@ -327,16 +327,16 @@ public struct ArticleCounts: Sendable {
/// Fetch the articleIDs of unread articles.
public func fetchUnreadArticleIDsAsync() async throws -> Set<String> {
try await withCheckedThrowingContinuation { continuation in
_fetchUnreadArticleIDsAsync { result in
continuation.resume(with: result)
_fetchUnreadArticleIDsAsync { articleIDs in
continuation.resume(returning: articleIDs)
}
}
}
public func fetchStarredArticleIDsAsync() async throws -> Set<String> {
try await withCheckedThrowingContinuation { continuation in
_fetchStarredArticleIDsAsync { result in
continuation.resume(with: result)
_fetchStarredArticleIDsAsync { articleIDs in
continuation.resume(returning: articleIDs)
}
}
}
@@ -344,8 +344,8 @@ public struct ArticleCounts: Sendable {
/// Fetch articleIDs for articles that we should have, but don’t. These articles are either starred or newer than the article cutoff date.
public func fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDateAsync() async throws -> Set<String> {
try await withCheckedThrowingContinuation { continuation in
_fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate { result in
continuation.resume(with: result)
_fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDate { articleIDs in
continuation.resume(returning: articleIDs)
}
}
}
@@ -354,16 +354,16 @@ public struct ArticleCounts: Sendable {
public func markAsync(articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws -> Set<ArticleStatus> {
try await withCheckedThrowingContinuation { continuation in
_mark(articles: articles, statusKey: statusKey, flag: flag) { result in
continuation.resume(with: result)
_mark(articles: articles, statusKey: statusKey, flag: flag) { statuses in
continuation.resume(returning: statuses)
}
}
}
public func markAndFetchNewAsync(articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool) async throws -> Set<String> {
try await withCheckedThrowingContinuation { continuation in
_markAndFetchNew(articleIDs: articleIDs, statusKey: statusKey, flag: flag) { result in
continuation.resume(with: result)
_markAndFetchNew(articleIDs: articleIDs, statusKey: statusKey, flag: flag) { articleIDs in
continuation.resume(returning: articleIDs)
}
}
}
@@ -444,20 +444,11 @@ private extension ArticlesDatabase {
// MARK: - Articles Table (Private)
typealias UnreadCountDictionaryCompletionBlock = @Sendable (UnreadCountDictionary) -> Void
typealias UpdateArticlesResult = Result<ArticleChanges, Error>
typealias UpdateArticlesCompletionBlock = @Sendable (UpdateArticlesResult) -> Void
typealias UpdateArticlesCompletionBlock = @Sendable (ArticleChanges) -> Void
typealias SingleUnreadCountCompletionBlock = @Sendable (Int) -> Void
typealias ArticleSetResult = Result<Set<Article>, Error>
typealias ArticleSetResultBlock = @Sendable (ArticleSetResult) -> Void
typealias ArticleIDsResult = Result<Set<String>, Error>
typealias ArticleIDsCompletionBlock = @Sendable (ArticleIDsResult) -> Void
typealias ArticleStatusesResult = Result<Set<ArticleStatus>, Error>
typealias ArticleStatusesResultBlock = @Sendable (ArticleStatusesResult) -> Void
typealias ArticleSetResultBlock = @Sendable (Set<Article>) -> Void
typealias ArticleIDsCompletionBlock = @Sendable (Set<String>) -> Void
typealias ArticleStatusesResultBlock = @Sendable (Set<ArticleStatus>) -> Void
private extension ArticlesDatabase {
@@ -132,7 +132,7 @@ final class ArticlesTable: DatabaseTable, Sendable {
// MARK: - Fetching Search Articles
func fetchArticlesMatching(_ searchString: String) throws -> Set<Article> {
func fetchArticlesMatching(_ searchString: String) -> Set<Article> {
nonisolated(unsafe) var articles: Set<Article> = Set<Article>()
queue.runInDatabaseSync { database in
@@ -142,14 +142,14 @@ final class ArticlesTable: DatabaseTable, Sendable {
return articles
}
func fetchArticlesMatching(_ searchString: String, _ feedIDs: Set<String>) throws -> Set<Article> {
var articles = try fetchArticlesMatching(searchString)
func fetchArticlesMatching(_ searchString: String, _ feedIDs: Set<String>) -> Set<Article> {
var articles = fetchArticlesMatching(searchString)
articles = articles.filter { feedIDs.contains($0.feedID) }
return articles
}
func fetchArticlesMatchingWithArticleIDs(_ searchString: String, _ articleIDs: Set<String>) throws -> Set<Article> {
var articles = try fetchArticlesMatching(searchString)
func fetchArticlesMatchingWithArticleIDs(_ searchString: String, _ articleIDs: Set<String>) -> Set<Article> {
var articles = fetchArticlesMatching(searchString)
articles = articles.filter { articleIDs.contains($0.articleID) }
return articles
}
@@ -464,7 +464,7 @@ final class ArticlesTable: DatabaseTable, Sendable {
self.queue.runInTransaction { database in
let statuses = self.statusesTable.mark(articles.statuses(), statusKey, flag, database)
DispatchQueue.main.async {
completion(.success(statuses ?? Set<ArticleStatus>()))
completion(statuses ?? Set<ArticleStatus>())
}
}
}
@@ -473,7 +473,7 @@ final class ArticlesTable: DatabaseTable, Sendable {
queue.runInTransaction { database in
let newStatusIDs = self.statusesTable.markAndFetchNew(articleIDs, statusKey, flag, database)
DispatchQueue.main.async {
completion(.success(newStatusIDs))
completion(newStatusIDs)
}
}
}
@@ -647,7 +647,7 @@ nonisolated private extension ArticlesTable {
queue.runInDatabase { database in
let articles = fetchMethod(database)
DispatchQueue.main.async {
completion(.success(articles))
completion(articles)
}
}
}
@@ -908,7 +908,7 @@ nonisolated private extension ArticlesTable {
func callUpdateArticlesCompletionBlock(_ newArticles: Set<Article>?, _ updatedArticles: Set<Article>?, _ deletedArticles: Set<Article>?, _ completion: @escaping UpdateArticlesCompletionBlock) {
let articleChanges = ArticleChanges(new: newArticles, updated: updatedArticles, deleted: deletedArticles)
DispatchQueue.main.async {
completion(.success(articleChanges))
completion(articleChanges)
}
}
@@ -111,14 +111,14 @@ final class StatusesTable: DatabaseTable, Sendable {
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
DispatchQueue.main.async {
completion(.success(Set<String>()))
completion(Set<String>())
}
return
}
let articleIDs = resultSet.mapToSet { $0.swiftString(forColumnIndex: 0) }
DispatchQueue.main.async {
completion(.success(articleIDs))
completion(articleIDs)
}
}
}
@@ -134,7 +134,7 @@ final class StatusesTable: DatabaseTable, Sendable {
}
DispatchQueue.main.async {
completion(.success(articleIDs))
completion(articleIDs)
}
}
}
+4 -5
View File
@@ -300,12 +300,11 @@ import Images
static func identifiers(for feed: Feed) -> [String] {
var ids = [String]()
ids.append(identifier(for: feed))
if let articles = try? feed.fetchArticles() {
for article in articles {
ids.append(identifier(for: article))
}
let articles = feed.fetchArticles()
for article in articles {
ids.append(identifier(for: article))
}
return ids
}
}
+8 -8
View File
@@ -85,20 +85,20 @@ import Images
extension SmartFeed: ArticleFetcher {
func fetchArticles() throws -> Set<Article> {
try delegate.fetchArticles()
func fetchArticles() -> Set<Article> {
delegate.fetchArticles()
}
func fetchArticlesAsync() async throws -> Set<Article> {
try await delegate.fetchArticlesAsync()
func fetchArticlesAsync() async -> Set<Article> {
await delegate.fetchArticlesAsync()
}
func fetchUnreadArticles() throws -> Set<Article> {
try delegate.fetchUnreadArticles()
func fetchUnreadArticles() -> Set<Article> {
delegate.fetchUnreadArticles()
}
func fetchUnreadArticlesAsync() async throws -> Set<Article> {
try await delegate.fetchUnreadArticlesAsync()
func fetchUnreadArticlesAsync() async -> Set<Article> {
await delegate.fetchUnreadArticlesAsync()
}
}
+8 -8
View File
@@ -19,20 +19,20 @@ import RSCore
@MainActor extension SmartFeedDelegate {
func fetchArticles() throws -> Set<Article> {
return try AccountManager.shared.fetchArticles(fetchType)
func fetchArticles() -> Set<Article> {
AccountManager.shared.fetchArticles(fetchType)
}
func fetchArticlesAsync() async throws -> Set<Article> {
try await AccountManager.shared.fetchArticlesAsync(fetchType)
func fetchArticlesAsync() async -> Set<Article> {
await AccountManager.shared.fetchArticlesAsync(fetchType)
}
func fetchUnreadArticles() throws -> Set<Article> {
try fetchArticles().unreadArticles()
func fetchUnreadArticles() -> Set<Article> {
fetchArticles().unreadArticles()
}
func fetchUnreadArticlesAsync() async throws -> Set<Article> {
let articles = try await fetchArticlesAsync()
func fetchUnreadArticlesAsync() async -> Set<Article> {
let articles = await fetchArticlesAsync()
return articles.unreadArticles()
}
}
+8 -8
View File
@@ -67,19 +67,19 @@ import Images
@MainActor extension UnreadFeed: ArticleFetcher {
func fetchArticles() throws -> Set<Article> {
return try fetchUnreadArticles()
func fetchArticles() -> Set<Article> {
fetchUnreadArticles()
}
func fetchArticlesAsync() async throws -> Set<Article> {
try await fetchUnreadArticlesAsync()
func fetchArticlesAsync() async -> Set<Article> {
await fetchUnreadArticlesAsync()
}
func fetchUnreadArticles() throws -> Set<Article> {
try AccountManager.shared.fetchArticles(fetchType)
func fetchUnreadArticles() -> Set<Article> {
AccountManager.shared.fetchArticles(fetchType)
}
func fetchUnreadArticlesAsync() async throws -> Set<Article> {
try await AccountManager.shared.fetchArticlesAsync(fetchType)
func fetchUnreadArticlesAsync() async -> Set<Article> {
await AccountManager.shared.fetchArticlesAsync(fetchType)
}
}
+10 -20
View File
@@ -94,17 +94,13 @@ typealias FetchRequestOperationResultBlock = (Set<Article>, FetchRequestOperatio
for fetcher in fetchers {
let articles: Set<Article>
do {
if (fetcher as? SidebarItem)?.readFiltered(readFilterEnabledTable: readFilterEnabledTable) ?? true {
articles = try await fetcher.fetchUnreadArticlesAsync()
} else {
articles = try await fetcher.fetchArticlesAsync()
}
} catch {
Self.logger.error("FetchRequestOperation \(self.id, privacy: .public): fetcher threw — \(String(describing: error), privacy: .public)")
Self.postFetchError(error)
articles = []
if (fetcher as? SidebarItem)?.readFiltered(readFilterEnabledTable: readFilterEnabledTable) ?? true {
articles = await fetcher.fetchUnreadArticlesAsync()
} else {
articles = await fetcher.fetchArticlesAsync()
}
process(articles)
}
@@ -193,16 +189,10 @@ typealias FetchRequestOperationResultBlock = (Set<Article>, FetchRequestOperatio
for fetcher in fetchers {
let articles: Set<Article>
do {
if fetcherHidesReadArticles(fetcher) {
articles = try await fetcher.fetchUnreadArticlesAsync()
} else {
articles = try await fetcher.fetchArticlesAsync()
}
} catch {
Self.logger.error("FetchRequestOperation \(self.id, privacy: .public): fetcher threw — \(String(describing: error), privacy: .public)")
Self.postFetchError(error)
articles = []
if fetcherHidesReadArticles(fetcher) {
articles = await fetcher.fetchUnreadArticlesAsync()
} else {
articles = await fetcher.fetchArticlesAsync()
}
process(articles)
}
+7 -15
View File
@@ -57,15 +57,7 @@ import Account
removeStaleFaviconsFromSharedContainer()
let latestData: WidgetData
do {
latestData = try fetchWidgetData()
Self.logger.debug("WidgetDataEncoder: fetched latest widget data")
} catch {
Self.logger.error("WidgetDataEncoder: error fetching widget data: \(error.localizedDescription)")
return
}
let latestData = fetchWidgetData()
let encodedData: Data
do {
encodedData = try JSONEncoder().encode(latestData)
@@ -118,15 +110,15 @@ import Account
@MainActor private extension WidgetDataEncoder {
func fetchWidgetData() throws -> WidgetData {
let fetchedUnreadArticles = try AccountManager.shared.fetchArticles(.unread(fetchLimit))
func fetchWidgetData() -> WidgetData {
let fetchedUnreadArticles = AccountManager.shared.fetchArticles(.unread(fetchLimit))
let unreadArticles = sortedLatestArticles(fetchedUnreadArticles)
let fetchedStarredArticles = try AccountManager.shared.fetchArticles(.starred(fetchLimit))
let fetchedStarredArticles = AccountManager.shared.fetchArticles(.starred(fetchLimit))
let starredArticles = sortedLatestArticles(fetchedStarredArticles)
let fetchedTodayArticles = try AccountManager.shared.fetchArticles(.today(fetchLimit))
let fetchedTodayTotal = try AccountManager.shared.fetchArticles(.today())
let fetchedTodayArticles = AccountManager.shared.fetchArticles(.today(fetchLimit))
let fetchedTodayTotal = AccountManager.shared.fetchArticles(.today())
let fetchedTodayTotalCount = fetchedTodayTotal.count
let fetchedTodayUnreadCount = fetchedTodayTotal.filter({ $0.status.read == false }).count
let todayArticles = sortedLatestArticles(fetchedTodayArticles)
@@ -134,7 +126,7 @@ import Account
let latestData = WidgetData(totalUnreadCount: SmartFeedsController.shared.unreadFeed.unreadCount,
totalTodayCount: fetchedTodayTotalCount,
totalTodayUnreadCount: fetchedTodayUnreadCount,
totalStarredCount: (try? AccountManager.shared.fetchCountForStarredArticles()) ?? 0,
totalStarredCount: AccountManager.shared.fetchCountForStarredArticles(),
unreadArticles: unreadArticles,
starredArticles: starredArticles,
todayArticles: todayArticles,
+1 -6
View File
@@ -458,12 +458,7 @@ private extension AppDelegate {
return
}
guard let singleArticleSet = try? account.fetchArticles(.articleIDs([articleID])) else {
assertionFailure("Expected article with \(articleID)")
Self.logger.error("No article with articleID found \(articleID) from status notification")
return
}
let singleArticleSet = account.fetchArticles(.articleIDs([articleID]))
assert(singleArticleSet.count == 1)
account.markArticles(singleArticleSet, statusKey: statusKey, flag: true) { _ in }
@@ -1132,8 +1132,11 @@ extension MainFeedCollectionViewController {
func markAllAsReadAlertAction(indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
guard let feed = dataSource.itemIdentifier(for: indexPath)?.node.representedObject as? Feed,
feed.unreadCount > 0,
let articles = try? feed.fetchArticles(), let contentView = self.collectionView.cellForItem(at: indexPath)?.contentView else {
feed.unreadCount > 0 else {
return nil
}
let articles = feed.fetchArticles()
guard let contentView = self.collectionView.cellForItem(at: indexPath)?.contentView else {
return nil
}
@@ -1229,9 +1232,8 @@ extension MainFeedCollectionViewController {
let title = NSString.localizedStringWithFormat(localizedMenuText as NSString, sidebarItem.nameForDisplay) as String
let action = UIAction(title: title, image: Assets.Images.markAllAsRead) { [weak self] _ in
MarkAsReadAlertController.confirm(self, coordinator: self?.coordinator, confirmTitle: title, sourceType: contentView) { [weak self] in
if let articles = try? sidebarItem.fetchUnreadArticles() {
self?.coordinator.markAllAsRead(Array(articles))
}
let articles = sidebarItem.fetchUnreadArticles()
self?.coordinator.markAllAsRead(Array(articles))
}
}
@@ -1249,9 +1251,8 @@ extension MainFeedCollectionViewController {
MarkAsReadAlertController.confirm(self, coordinator: self?.coordinator, confirmTitle: title, sourceType: contentView) { [weak self] in
// If you don't have this delay the screen flashes when it executes this code
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
if let articles = try? account.fetchArticles(.unread()) {
self?.coordinator.markAllAsRead(Array(articles))
}
let articles = account.fetchArticles(.unread())
self?.coordinator.markAllAsRead(Array(articles))
}
}
}
@@ -1299,11 +1299,11 @@ extension MainTimelineModernViewController {
}
func markAllInFeedAsReadAction(_ article: Article, indexPath: IndexPath) -> UIAction? {
guard let feed = article.feed else { return nil }
guard let fetchedArticles = try? feed.fetchArticles() else {
guard let feed = article.feed else {
return nil
}
let fetchedArticles = feed.fetchArticles()
let articles = Array(fetchedArticles)
guard articles.canMarkAllAsRead(), let collectionView, let contentView = collectionView.cellForItem(at: indexPath)?.contentView else {
return nil
@@ -1321,11 +1321,11 @@ extension MainTimelineModernViewController {
}
func markAllInFeedAsReadAlertAction(_ article: Article, indexPath: IndexPath, completion: @escaping (Bool) -> Void) -> UIAlertAction? {
guard let feed = article.feed else { return nil }
guard let fetchedArticles = try? feed.fetchArticles() else {
guard let feed = article.feed else {
return nil
}
let fetchedArticles = feed.fetchArticles()
let articles = Array(fetchedArticles)
guard articles.canMarkAllAsRead(), let collectionView, let contentView = collectionView.cellForItem(at: indexPath)?.contentView else {
return nil