Created
August 30, 2025 10:53
-
-
Save yccheok/1b9a696c0ffc7945aaa26e49097051e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static func deletePersistentHistoryIfPossible() { | |
| let ONE_DAY_DURATION: Int64 = 1*24*60*60*1000 | |
| let SEVEN_DAYS_DURATION: Int64 = 7*24*60*60*1000 | |
| let FOURTEEN_DAYS_DURATION: Int64 = 14*24*60*60*1000 | |
| let lastDeletePersistentHistoryTimestamp = WeNoteOptions.lastDeletePersistentHistoryTimestamp | |
| let currentTimeMillis = Date.currentTimeMillis | |
| let iCloudLastSyncInfo = WeNoteOptions.INSTANCE.iCloudLastSyncInfo | |
| if !WeNoteOptions.isiCloudSync { | |
| // https://stackoverflow.com/questions/72733892/how-should-we-handle-history-tracking-transactions-purging-if-we-allow-users-to | |
| return | |
| } | |
| if iCloudLastSyncInfo.timestamp <= 0 { | |
| // We have never success in iCloud sync. | |
| return | |
| } | |
| if currentTimeMillis <= FOURTEEN_DAYS_DURATION { | |
| // Something went wrong on currentTimeMillis. | |
| return | |
| } | |
| if lastDeletePersistentHistoryTimestamp <= 0 { | |
| // First time to perform WeNoteOptions.lastDeletePersistentHistoryTimestamp update. | |
| WeNoteOptions.lastDeletePersistentHistoryTimestamp = currentTimeMillis | |
| return | |
| } | |
| if lastDeletePersistentHistoryTimestamp > currentTimeMillis { | |
| // Perhaps previous WeNoteOptions.lastDeletePersistentHistoryTimestamp is holding wrong information. | |
| WeNoteOptions.lastDeletePersistentHistoryTimestamp = currentTimeMillis | |
| return | |
| } | |
| let duration = currentTimeMillis - lastDeletePersistentHistoryTimestamp | |
| if duration < ONE_DAY_DURATION { | |
| // Return early without updating WeNoteOptions.lastDeletePersistentHistoryTimestamp. Avoding performing | |
| // deleteHistory too frequent. | |
| return | |
| } | |
| // https://stackoverflow.com/questions/72557060/what-is-the-right-way-to-perform-persistent-history-purging-without-affecting-t | |
| if duration > (SEVEN_DAYS_DURATION) { | |
| // The user hasn't launched the app for more than 7 days. Most probably CloudKit has not yet consume the | |
| // history transactions. Update WeNoteOptions.lastDeletePersistentHistoryTimestamp then return early. | |
| WeNoteOptions.lastDeletePersistentHistoryTimestamp = currentTimeMillis | |
| return | |
| } | |
| let date0 = Date(timeMillis: currentTimeMillis - FOURTEEN_DAYS_DURATION) | |
| let date1: Date | |
| if (iCloudLastSyncInfo.timestamp - SEVEN_DAYS_DURATION) > 0 { | |
| date1 = Date(timeMillis: iCloudLastSyncInfo.timestamp - SEVEN_DAYS_DURATION) | |
| } else { | |
| date1 = date0 | |
| } | |
| let backgroundContext = CoreDataStack.INSTANCE.backgroundContext | |
| backgroundContext.performAndWait { | |
| let bestDate = date0 < date1 ? date0 : date1 | |
| let purgeHistoryRequest = NSPersistentHistoryChangeRequest.deleteHistory(before: bestDate) | |
| do { | |
| try backgroundContext.execute(purgeHistoryRequest) | |
| } catch { | |
| error_log(error) | |
| } | |
| } | |
| WeNoteOptions.lastDeletePersistentHistoryTimestamp = currentTimeMillis | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment