Keep Monero scan progress across app restarts

This commit is contained in:
2026-09-19 22:17:00 +08:00
parent f4037a188a
commit 807388ea6b
4 changed files with 60 additions and 19 deletions
+19
View File
@@ -21,6 +21,7 @@ final class AppModel: ObservableObject {
private var refreshTick = 0
private var refreshTask: Task<Void, Never>?
private var lastSample: (height: UInt64, date: Date)?
private var lastCheckpoint = Date.distantPast
var wallet: WalletRecord? {
chain == .xrp ? xrpWallet : xmrWallet
@@ -212,6 +213,7 @@ final class AppModel: ObservableObject {
// progress current here too.
if moneroReady, let status = try? await MoneroService.shared.syncStatus() {
updateSyncStatus(status)
await checkpointWalletIfSyncing()
}
return
}
@@ -222,6 +224,7 @@ final class AppModel: ObservableObject {
// list touch the wallet's data files, so they are refreshed less often.
updateSyncStatus(try await MoneroService.shared.syncStatus())
refreshTick += 1
await checkpointWalletIfSyncing()
if force || refreshTick == 1 || refreshTick % 4 == 0 {
async let balanceCall = MoneroService.shared.balance()
async let txCall = MoneroService.shared.transactions()
@@ -334,4 +337,20 @@ final class AppModel: ObservableObject {
errorMessage = nil
statusMessage = nil
}
/// Called when the app leaves the foreground: iOS can suspend or kill it at
/// any point, so flush the wallet while there is still time.
func persistWallet() async {
guard moneroReady else { return }
await MoneroService.shared.save()
}
/// monero-cpp only writes the wallet file from save(), and its sync loop
/// never does, so without this a restart re-scans from the beginning.
private func checkpointWalletIfSyncing() async {
guard moneroReady, syncStatus?.syncing == true else { return }
guard Date().timeIntervalSince(lastCheckpoint) >= 60 else { return }
lastCheckpoint = Date()
await MoneroService.shared.save()
}
}