diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index f6c069760..ab6b5f8d4 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -615,6 +615,11 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } } + /// Empty caches that can reasonably be emptied. Call when the app goes in the background, for instance. + func emptyCaches() { + database.emptyCaches() + } + // MARK: - Container public func flattenedFeeds() -> Set { diff --git a/Frameworks/Account/AccountManager.swift b/Frameworks/Account/AccountManager.swift index 0b5e71b20..a56336673 100644 --- a/Frameworks/Account/AccountManager.swift +++ b/Frameworks/Account/AccountManager.swift @@ -223,6 +223,15 @@ public final class AccountManager: UnreadCountProvider { } } + // MARK: - Caches + + /// Empty caches that can reasonably be emptied — when the app moves to the background, for instance. + public func emptyCaches() { + for account in accounts { + account.emptyCaches() + } + } + // MARK: - Notifications @objc dynamic func unreadCountDidChange(_ notification: Notification) { diff --git a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift index d3e455e36..b5c4343a1 100644 --- a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift +++ b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift @@ -143,6 +143,14 @@ public final class ArticlesDatabase { public func mark(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) -> Set? { return articlesTable.mark(articles, statusKey, flag) } + + // MARK: - Caches + + /// Call to free up some memory. Should be done when the app is backgrounded, for instance. + /// This does not empty *all* caches — just the ones that are empty-able. + public func emptyCaches() { + articlesTable.emptyCaches() + } } // MARK: - Private diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index 8f06afdc8..7d8808339 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -411,6 +411,14 @@ final class ArticlesTable: DatabaseTable { } } } + + // MARK: - Caches + + func emptyCaches() { + queue.run { _ in + self.databaseArticlesCache = [String: DatabaseArticle]() + } + } } // MARK: - Private diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 81fbddd7d..4270d4899 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -224,9 +224,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, } func applicationDidResignActive(_ notification: Notification) { - TimelineStringFormatter.emptyCaches() - + AccountManager.shared.emptyCaches() saveState() }