Purge old persistent history after processing

Closes #480
This commit is contained in:
Shadowfacts 2024-06-08 12:23:12 -07:00
parent 6528070f1c
commit d8fccc8f1b
1 changed files with 27 additions and 4 deletions

View File

@ -530,11 +530,34 @@ class MastodonCachePersistentStore: NSPersistentCloudKitContainer {
defer { defer {
PersistentHistoryTokenStore.setToken(token, for: accountInfo) PersistentHistoryTokenStore.setToken(token, for: accountInfo)
} }
let transactions: [NSPersistentHistoryTransaction]
do {
let req = NSPersistentHistoryChangeRequest.fetchHistory(after: lastToken) let req = NSPersistentHistoryChangeRequest.fetchHistory(after: lastToken)
if let result = try? self.remoteChangesBackgroundContext.execute(req) as? NSPersistentHistoryResult, if let result = try self.remoteChangesBackgroundContext.execute(req) as? NSPersistentHistoryResult {
let transactions = result.result as? [NSPersistentHistoryTransaction], transactions = result.result as? [NSPersistentHistoryTransaction] ?? []
!transactions.isEmpty { } else {
logger.error("Unexpectedly non-NSPersistentHistoryResult")
return
}
} catch {
logger.error("Unable to fetch persistent history results: \(String(describing: error), privacy: .public)")
return
}
if !transactions.isEmpty {
self.processPersistentHistoryTransactions(transactions) self.processPersistentHistoryTransactions(transactions)
do {
// delete history only before the last token, because we don't want to invalidate
// the most recent token
let deleteReq = NSPersistentHistoryChangeRequest.deleteHistory(before: lastToken)
let result = try self.remoteChangesBackgroundContext.execute(deleteReq)
if let result = result as? NSPersistentHistoryResult,
result.resultType == .statusOnly {
logger.info("Delete old persistent history result status: \(result.result as! Int)")
}
} catch {
logger.error("Unable to delete old persistent history: \(String(describing: error), privacy: .public)")
}
} }
} }
} }