mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-29 00:31:14 +00:00
Fix hang in WidgetDataEncoder by fetching async. Use count queries in places where we were fetching articles just to get counts.
This commit is contained in:
1 parent
f509851ddc
commit
eb82dd3ce3
6 files changed
+125
-13
No files matched your search
@@ -884,6 +884,14 @@ public enum FetchType {
|
||||
await database.fetchUnreadCountForTodayAsync(feedIDs: flattenedFeedsIDs)
|
||||
}
|
||||
|
||||
public func fetchCountForTodayArticlesAsync() async -> Int {
|
||||
await database.fetchTodayArticlesCountAsync(feedIDs: flattenedFeedsIDs)
|
||||
}
|
||||
|
||||
public func fetchCountForStarredArticlesAsync() async -> Int {
|
||||
await database.fetchStarredArticlesCountAsync(feedIDs: flattenedFeedsIDs)
|
||||
}
|
||||
|
||||
public func fetchUnreadArticleIDsAsync() async -> Set<String> {
|
||||
await database.fetchUnreadArticleIDsAsync()
|
||||
}
|
||||
|
||||
@@ -441,6 +441,33 @@ import ActivityLog
|
||||
return count
|
||||
}
|
||||
|
||||
public func fetchCountForStarredArticlesAsync() async -> Int {
|
||||
precondition(Thread.isMainThread)
|
||||
var count = 0
|
||||
for account in activeAccounts {
|
||||
count += await account.fetchCountForStarredArticlesAsync()
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
public func fetchCountForTodayArticlesAsync() async -> Int {
|
||||
precondition(Thread.isMainThread)
|
||||
var count = 0
|
||||
for account in activeAccounts {
|
||||
count += await account.fetchCountForTodayArticlesAsync()
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
public func fetchUnreadCountForTodayAsync() async -> Int {
|
||||
precondition(Thread.isMainThread)
|
||||
var count = 0
|
||||
for account in activeAccounts {
|
||||
count += await account.fetchUnreadCountForTodayAsync()
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// MARK: - Vacuum
|
||||
|
||||
public func vacuumAllDatabases() async {
|
||||
|
||||
@@ -289,6 +289,22 @@ public struct ArticleCounts: Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
public func fetchTodayArticlesCountAsync(feedIDs: Set<String>) async -> Int {
|
||||
await withCheckedContinuation { continuation in
|
||||
articlesTable.fetchArticlesCountSince(feedIDs, todayCutoffDate()) { count in
|
||||
continuation.resume(returning: count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func fetchStarredArticlesCountAsync(feedIDs: Set<String>) async -> Int {
|
||||
await withCheckedContinuation { continuation in
|
||||
articlesTable.fetchStarredArticlesCountAsync(feedIDs) { count in
|
||||
continuation.resume(returning: count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Saving, Updating, and Deleting Articles
|
||||
|
||||
/// Update articles and save new ones — for feed-based systems (local and iCloud).
|
||||
|
||||
@@ -442,6 +442,49 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
func fetchArticlesCountSince(_ feedIDs: Set<String>, _ cutoffDate: Date, _ completion: @escaping SingleUnreadCountCompletionBlock) {
|
||||
// Total count (read and unread) since a cutoff date — today's count, for instance.
|
||||
if feedIDs.isEmpty {
|
||||
completion(0)
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInDatabase { database in
|
||||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))!
|
||||
let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and (datePublished > ? or (datePublished is null and dateArrived > ?));"
|
||||
|
||||
var parameters = [Any]()
|
||||
parameters += Array(feedIDs) as [Any]
|
||||
parameters += [cutoffDate] as [Any]
|
||||
parameters += [cutoffDate] as [Any]
|
||||
|
||||
let count = self.numberWithSQLAndParameters(sql, parameters, in: database)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
completion(count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fetchStarredArticlesCountAsync(_ feedIDs: Set<String>, _ completion: @escaping SingleUnreadCountCompletionBlock) {
|
||||
if feedIDs.isEmpty {
|
||||
completion(0)
|
||||
return
|
||||
}
|
||||
|
||||
queue.runInDatabase { database in
|
||||
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))!
|
||||
let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and starred=1;"
|
||||
let parameters = Array(feedIDs) as [Any]
|
||||
|
||||
let count = self.numberWithSQLAndParameters(sql, parameters, in: database)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
completion(count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Statuses
|
||||
|
||||
func fetchUnreadArticleIDsAsync(_ completion: @escaping ArticleIDsCompletionBlock) {
|
||||
|
||||
@@ -46,18 +46,35 @@ import Account
|
||||
}
|
||||
}
|
||||
|
||||
/// Fire-and-forget. Sets `isRunning` synchronously so the background-task wait loop
|
||||
/// (see AppDelegate.waitToComplete) holds the app awake until encoding finishes.
|
||||
func encode() {
|
||||
guard !isRunning else {
|
||||
Self.logger.debug("WidgetDataEncoder: skipping encode because already in encode")
|
||||
return
|
||||
}
|
||||
|
||||
isRunning = true
|
||||
Task { @MainActor in
|
||||
await performEncode()
|
||||
}
|
||||
}
|
||||
|
||||
/// Awaitable variant for callers that must finish encoding before suspending the app.
|
||||
func encodeAndWait() async {
|
||||
guard !isRunning else {
|
||||
Self.logger.debug("WidgetDataEncoder: skipping encode because already in encode")
|
||||
return
|
||||
}
|
||||
isRunning = true
|
||||
await performEncode()
|
||||
}
|
||||
|
||||
private func performEncode() async {
|
||||
defer { isRunning = false }
|
||||
|
||||
removeStaleFaviconsFromSharedContainer()
|
||||
|
||||
let latestData = fetchWidgetData()
|
||||
let latestData = await fetchWidgetData()
|
||||
let encodedData: Data
|
||||
do {
|
||||
encodedData = try JSONEncoder().encode(latestData)
|
||||
@@ -110,23 +127,24 @@ import Account
|
||||
|
||||
@MainActor private extension WidgetDataEncoder {
|
||||
|
||||
func fetchWidgetData() -> WidgetData {
|
||||
let fetchedUnreadArticles = AccountManager.shared.fetchArticles(.unread(fetchLimit))
|
||||
func fetchWidgetData() async -> WidgetData {
|
||||
let fetchedUnreadArticles = await AccountManager.shared.fetchArticlesAsync(.unread(fetchLimit))
|
||||
let unreadArticles = sortedLatestArticles(fetchedUnreadArticles)
|
||||
|
||||
let fetchedStarredArticles = AccountManager.shared.fetchArticles(.starred(fetchLimit))
|
||||
let fetchedStarredArticles = await AccountManager.shared.fetchArticlesAsync(.starred(fetchLimit))
|
||||
let starredArticles = sortedLatestArticles(fetchedStarredArticles)
|
||||
|
||||
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 fetchedTodayArticles = await AccountManager.shared.fetchArticlesAsync(.today(fetchLimit))
|
||||
let todayArticles = sortedLatestArticles(fetchedTodayArticles)
|
||||
|
||||
let totalTodayCount = await AccountManager.shared.fetchCountForTodayArticlesAsync()
|
||||
let totalTodayUnreadCount = await AccountManager.shared.fetchUnreadCountForTodayAsync()
|
||||
let totalStarredCount = await AccountManager.shared.fetchCountForStarredArticlesAsync()
|
||||
|
||||
let latestData = WidgetData(totalUnreadCount: SmartFeedsController.shared.unreadFeed.unreadCount,
|
||||
totalTodayCount: fetchedTodayTotalCount,
|
||||
totalTodayUnreadCount: fetchedTodayUnreadCount,
|
||||
totalStarredCount: AccountManager.shared.fetchCountForStarredArticles(),
|
||||
totalTodayCount: totalTodayCount,
|
||||
totalTodayUnreadCount: totalTodayUnreadCount,
|
||||
totalStarredCount: totalStarredCount,
|
||||
unreadArticles: unreadArticles,
|
||||
starredArticles: starredArticles,
|
||||
todayArticles: todayArticles,
|
||||
|
||||
@@ -413,7 +413,7 @@ private extension AppDelegate {
|
||||
}
|
||||
await AccountManager.shared.refreshAll(errorHandler: ErrorHandler.log)
|
||||
if !AccountManager.shared.isSuspended {
|
||||
WidgetDataEncoder.shared?.encode()
|
||||
await WidgetDataEncoder.shared?.encodeAndWait()
|
||||
self.suspendApplication()
|
||||
Self.logger.info("Background refresh completed.")
|
||||
task.setTaskCompleted(success: true)
|
||||
|
||||
Reference in new issue
Block a user