Add cache reset button to Advanced Preferences

This commit is contained in:
Shadowfacts 2020-05-13 18:58:11 -04:00
parent 0582812563
commit 35a510e8ed
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 32 additions and 6 deletions

View File

@ -30,6 +30,10 @@ class MastodonController {
}
}
static func resetAll() {
all = [:]
}
private let transient: Bool
private(set) lazy var persistentContainer = MastodonCachePersistentStore(for: accountInfo, transient: transient)

View File

@ -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()
}
}

View File

@ -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 {

View File

@ -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()
}

View File

@ -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()

View File

@ -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()
}
}
}