From 4df25ae2f6a1184c85d918fb3596c30d46e3ce15 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Sat, 16 Oct 2021 13:20:37 +0800 Subject: [PATCH] Potential fix for #3200 --- Shared/Widget/WidgetDataEncoder.swift | 110 ++++++++++++-------------- 1 file changed, 50 insertions(+), 60 deletions(-) diff --git a/Shared/Widget/WidgetDataEncoder.swift b/Shared/Widget/WidgetDataEncoder.swift index 86c46be3b..575848eb4 100644 --- a/Shared/Widget/WidgetDataEncoder.swift +++ b/Shared/Widget/WidgetDataEncoder.swift @@ -30,65 +30,59 @@ public final class WidgetDataEncoder { @available(iOS 14, *) func encodeWidgetData() throws { + os_log(.debug, log: log, "Starting encoding widget data.") - do { - let unreadArticles = Array(try AccountManager.shared.fetchArticles(.unread(fetchLimit))).sortedByDate(.orderedDescending) - let starredArticles = Array(try AccountManager.shared.fetchArticles(.starred(fetchLimit))).sortedByDate(.orderedDescending) - let todayArticles = Array(try AccountManager.shared.fetchArticles(.today(fetchLimit))).sortedByDate(.orderedDescending) - - var unread = [LatestArticle]() - var today = [LatestArticle]() - var starred = [LatestArticle]() - - for article in unreadArticles { - let latestArticle = LatestArticle(id: article.sortableArticleID, - feedTitle: article.sortableName, - articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article), - articleSummary: article.summary, - feedIcon: article.iconImage()?.image.dataRepresentation(), - pubDate: article.datePublished?.description ?? "") - unread.append(latestArticle) - } - - for article in starredArticles { - let latestArticle = LatestArticle(id: article.sortableArticleID, - feedTitle: article.sortableName, - articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article), - articleSummary: article.summary, - feedIcon: article.iconImage()?.image.dataRepresentation(), - pubDate: article.datePublished?.description ?? "") - starred.append(latestArticle) - } - - for article in todayArticles { - let latestArticle = LatestArticle(id: article.sortableArticleID, - feedTitle: article.sortableName, - articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article), - articleSummary: article.summary, - feedIcon: article.iconImage()?.image.dataRepresentation(), - pubDate: article.datePublished?.description ?? "") - today.append(latestArticle) - } - - let latestData = WidgetData(currentUnreadCount: SmartFeedsController.shared.unreadFeed.unreadCount, - currentTodayCount: SmartFeedsController.shared.todayFeed.unreadCount, - currentStarredCount: try! SmartFeedsController.shared.starredFeed.fetchArticles().count, - unreadArticles: unread, - starredArticles: starred, - todayArticles:today, - lastUpdateTime: Date()) - - - DispatchQueue.global().async { [weak self] in - guard let self = self else { return } + DispatchQueue.main.async { + do { + let unreadArticles = Array(try AccountManager.shared.fetchArticles(.unread(self.fetchLimit))).sortedByDate(.orderedDescending) + let starredArticles = Array(try AccountManager.shared.fetchArticles(.starred(self.fetchLimit))).sortedByDate(.orderedDescending) + let todayArticles = Array(try AccountManager.shared.fetchArticles(.today(self.fetchLimit))).sortedByDate(.orderedDescending) - self.backgroundTaskID = UIApplication.shared.beginBackgroundTask (withName: "com.ranchero.NetNewsWire.Encode") { - UIApplication.shared.endBackgroundTask(self.backgroundTaskID!) - self.backgroundTaskID = .invalid + var unread = [LatestArticle]() + var today = [LatestArticle]() + var starred = [LatestArticle]() + + for article in unreadArticles { + let latestArticle = LatestArticle(id: article.sortableArticleID, + feedTitle: article.sortableName, + articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article), + articleSummary: article.summary, + feedIcon: article.iconImage()?.image.dataRepresentation(), + pubDate: article.datePublished?.description ?? "") + unread.append(latestArticle) } - let encodedData = try? JSONEncoder().encode(latestData) + for article in starredArticles { + let latestArticle = LatestArticle(id: article.sortableArticleID, + feedTitle: article.sortableName, + articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article), + articleSummary: article.summary, + feedIcon: article.iconImage()?.image.dataRepresentation(), + pubDate: article.datePublished?.description ?? "") + starred.append(latestArticle) + } + + for article in todayArticles { + let latestArticle = LatestArticle(id: article.sortableArticleID, + feedTitle: article.sortableName, + articleTitle: ArticleStringFormatter.truncatedTitle(article).isEmpty ? ArticleStringFormatter.truncatedSummary(article) : ArticleStringFormatter.truncatedTitle(article), + articleSummary: article.summary, + feedIcon: article.iconImage()?.image.dataRepresentation(), + pubDate: article.datePublished?.description ?? "") + today.append(latestArticle) + } + + let latestData = WidgetData(currentUnreadCount: SmartFeedsController.shared.unreadFeed.unreadCount, + currentTodayCount: SmartFeedsController.shared.todayFeed.unreadCount, + currentStarredCount: try! SmartFeedsController.shared.starredFeed.fetchArticles().count, + unreadArticles: unread, + starredArticles: starred, + todayArticles:today, + lastUpdateTime: Date()) + + + let encodedData = try JSONEncoder().encode(latestData) os_log(.debug, log: self.log, "Finished encoding widget data.") if self.fileExists() { @@ -98,13 +92,9 @@ public final class WidgetDataEncoder { if FileManager.default.createFile(atPath: self.dataURL!.path, contents: encodedData, attributes: nil) { os_log(.debug, log: self.log, "Wrote widget data to container.") WidgetCenter.shared.reloadAllTimelines() - UIApplication.shared.endBackgroundTask(self.backgroundTaskID!) - self.backgroundTaskID = .invalid - } else { - UIApplication.shared.endBackgroundTask(self.backgroundTaskID!) - self.backgroundTaskID = .invalid } - + } catch { + print(error.localizedDescription) } } }