diff --git a/Tusker/Controllers/MastodonController.swift b/Tusker/Controllers/MastodonController.swift index a75a0eccfa..31dd97b8b2 100644 --- a/Tusker/Controllers/MastodonController.swift +++ b/Tusker/Controllers/MastodonController.swift @@ -30,6 +30,10 @@ class MastodonController { } } + static func resetAll() { + all = [:] + } + private let transient: Bool private(set) lazy var persistentContainer = MastodonCachePersistentStore(for: accountInfo, transient: transient) diff --git a/Tusker/Screens/Conversation/ConversationTableViewController.swift b/Tusker/Screens/Conversation/ConversationTableViewController.swift index 85ab426fcd..1712423a99 100644 --- a/Tusker/Screens/Conversation/ConversationTableViewController.swift +++ b/Tusker/Screens/Conversation/ConversationTableViewController.swift @@ -43,8 +43,9 @@ class ConversationTableViewController: EnhancedTableViewController { } deinit { + guard let persistentContainer = mastodonController?.persistentContainer else { return } for (id, _) in statuses { - mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount() + persistentContainer.status(for: id)?.decrementReferenceCount() } } diff --git a/Tusker/Screens/Preferences/AdvancedPrefsView.swift b/Tusker/Screens/Preferences/AdvancedPrefsView.swift index fc82c27bd3..5ab1d6c111 100644 --- a/Tusker/Screens/Preferences/AdvancedPrefsView.swift +++ b/Tusker/Screens/Preferences/AdvancedPrefsView.swift @@ -15,6 +15,7 @@ struct AdvancedPrefsView : View { List { formattingSection automationSection + cachingSection }.listStyle(GroupedListStyle()) .navigationBarTitle(Text("Advanced")) } @@ -41,6 +42,27 @@ struct AdvancedPrefsView : View { } } } + + var cachingSection: some View { + Section(header: Text("CACHING")) { + Button(action: clearCache) { + Text("Clear Cache") + }.foregroundColor(.red) + } + } + + func clearCache() { + for account in LocalData.shared.accounts { + let controller = MastodonController.getForAccount(account) + let coordinator = controller.persistentContainer.persistentStoreCoordinator + for store in coordinator.persistentStores { + try! coordinator.destroyPersistentStore(at: store.url!, ofType: store.type, options: store.options) + } + } + MastodonController.resetAll() + let mostRecent = LocalData.shared.getMostRecentAccount()! + NotificationCenter.default.post(name: .activateAccount, object: nil, userInfo: ["account": mostRecent]) + } } extension StatusContentType { diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index b5e76b1325..85890d16ea 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -62,8 +62,7 @@ class ProfileTableViewController: EnhancedTableViewController { } deinit { - if let id = accountID { - let container = mastodonController.persistentContainer + if let id = accountID, let container = mastodonController?.persistentContainer { container.backgroundContext.perform { container.account(for: id, in: container.backgroundContext)?.decrementReferenceCount() } diff --git a/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift b/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift index b7595a92df..b4f2ff7442 100644 --- a/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift +++ b/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift @@ -59,8 +59,7 @@ class StatusActionAccountListTableViewController: EnhancedTableViewController { } deinit { - if let accountIDs = self.accountIDs { - let container = self.mastodonController.persistentContainer + if let accountIDs = self.accountIDs, let container = mastodonController?.persistentContainer { container.backgroundContext.perform { for id in accountIDs { container.account(for: id, in: container.backgroundContext)?.decrementReferenceCount() diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index ddc1b908e4..4a9a8d985b 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -39,12 +39,13 @@ class TimelineTableViewController: EnhancedTableViewController { } deinit { + guard let persistentContainer = mastodonController?.persistentContainer else { return } // decrement reference counts of any statuses we still have // if the app is currently being quit, this will not affect the persisted data because // the persistent container would already have been saved in SceneDelegate.sceneDidEnterBackground(_:) for segment in timelineSegments { for (id, _) in segment { - mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount() + persistentContainer.status(for: id)?.decrementReferenceCount() } } }