From 35adaa7a8ce74a9eff079c988ada58ff7fb02b7b Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 29 Jan 2020 23:09:38 -0800 Subject: [PATCH] =?UTF-8?q?Fetch=20the=20unread=20count=20for=20just=20one?= =?UTF-8?q?=20feed=20when=20that=E2=80=99s=20all=20that=E2=80=99s=20needed?= =?UTF-8?q?.=20Helps=20performance.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frameworks/Account/Account.swift | 42 +++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index c6d6a220a..f2c11aa1b 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -234,7 +234,9 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, private var operationQueue = MainThreadOperationQueue() private enum OperationName { static let FetchAllUnreadCounts = "FetchAllUnreadCounts" + static let FetchFeedUnreadCount = "FetchFeedUnreadCount" } + private static let discardableOperationNames = [OperationName.FetchAllUnreadCounts, OperationName.FetchFeedUnreadCount] init?(dataFolder: String, type: AccountType, accountID: String, transport: Transport? = nil) { switch type { @@ -444,6 +446,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, metadataFile.load() webFeedMetadataFile.load() opmlFile.load() + fetchAllUnreadCounts() } public func save() { @@ -457,7 +460,9 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } public func cancelDiscardableOperations() { - operationQueue.cancelOperations(named: OperationName.FetchAllUnreadCounts) + for operationName in Self.discardableOperationNames { + operationQueue.cancelOperations(named: operationName) + } } func loadOPMLItems(_ items: [RSOPMLItem], parentFolder: Folder?) { @@ -621,17 +626,22 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, completion?() return } - - database.fetchUnreadCounts(for: webFeeds.webFeedIDs()) { unreadCountDictionaryResult in - if let unreadCountDictionary = try? unreadCountDictionaryResult.get() { - for webFeed in webFeeds { - if let unreadCount = unreadCountDictionary[webFeed.webFeedID] { - webFeed.unreadCount = unreadCount + + if webFeeds.count == 1, let feed = webFeeds.first { + fetchUnreadCount(feed, completion) + } + else { + database.fetchUnreadCounts(for: webFeeds.webFeedIDs()) { unreadCountDictionaryResult in + if let unreadCountDictionary = try? unreadCountDictionaryResult.get() { + for webFeed in webFeeds { + if let unreadCount = unreadCountDictionary[webFeed.webFeedID] { + webFeed.unreadCount = unreadCount + } } } - } - completion?() + completion?() + } } } @@ -1236,6 +1246,20 @@ private extension Account { NotificationCenter.default.post(name: .StatusesDidChange, object: self, userInfo: [UserInfoKey.articleIDs: articleIDs]) } + func fetchUnreadCount(_ feed: WebFeed, _ completion: VoidCompletionBlock?) { + let operation = database.createFetchFeedUnreadCountOperation(feedID: feed.webFeedID) + operation.name = OperationName.FetchFeedUnreadCount + operation.completionBlock = { operation in + let fetchOperation = operation as! FetchFeedUnreadCountOperation + if let unreadCount = fetchOperation.unreadCount { + feed.unreadCount = unreadCount + } + completion?() + } + + operationQueue.addOperation(operation) + } + func fetchAllUnreadCounts() { fetchingAllUnreadCounts = true operationQueue.cancelOperations(named: OperationName.FetchAllUnreadCounts)