perf: eliminate full rescans with local caches; add queue tracking, notifications, SyncView redesign

Cached indexes:
- NASManifestCache: actor-based disk cache for the NAS manifest (kisani_manifest_cache.json)
  Avoids NAS network round-trip when cache < 5 min old. Updated after every manifest download.
- LocalPhotoIndex: actor-based disk cache of phone asset metadata (kisani_local_index.json)
  Built once on first launch, then updated incrementally via PHPhotoLibraryChangeObserver.
  O(1) countSafe via in-memory dict (vs PHFetchResult batch enumeration + CoreData calls).

Fast path reconcile:
  When both caches are warm, reconcile() skips NAS connection + PHFetchResult enumeration
  entirely and returns in microseconds. Falls through to full NAS path only when stale.

AutoBackupCoordinator.onActive():
  Changed force: true → force: false so the 30-second debounce prevents expensive rescans
  on rapid app foreground/background cycles. Dashboard appears instantly from cached snapshot.

BackupQueueItem + queue tracking:
  BackupEngine publishes queueItems: [BackupQueueItem] with per-file status, progress,
  speed, error details. Queue is persisted to disk and reloaded on next launch so SyncView
  always has data to show even after restart.

NotificationService:
  Centralised local notification sender replacing the inline UNMutableNotificationContent
  blocks. Sends for: backup started, completed, failed, paused, NAS offline. Uses category
  identifiers so same-type notifications replace each other.

SyncView redesign:
  - Removed pill/chip around NAS IP — replaced with plain "Online/Offline/Checking" dot+text
  - No live NAS directory listing (was expensive NAS connection on every tab open)
  - Shows engine.queueItems grouped into Active / Failed / Completed sections
  - Per-file rows with inline progress bar + bytes/speed for uploading items, error text for failed
  - Idle state shows archive count from statusService snapshot

GalleryViewModel:
  NAS tab and All tab now use NASManifestCache as fallback when statusService.lastManifest
  is nil (offline launch). Gallery shows NAS items even when NAS is unreachable.

AppDelegate:
  Bootstraps LocalPhotoIndex.loadOrBuild() in background on launch (non-blocking).

Co-Authored-By: Kutesir <kutesir@provoc.ug>
Co-Authored-By: Sentry <sentry@provoc.ug>
This commit is contained in:
Robin Kutesa
2026-05-17 23:28:29 +03:00
parent 025326f2b2
commit 1afcbaff96
11 changed files with 786 additions and 325 deletions

View File

@@ -71,9 +71,12 @@ final class AutoBackupCoordinator: ObservableObject {
func onActive() {
guard !engine.job.status.isActive else { return }
guard store.savedConnection != nil else { return }
guard phase == .idle else { return } // Don't interrupt an in-progress check
phase = .checking
statusService.refresh(force: true)
// handleReconciliationComplete() fires via Combine when the refresh settles
// force: false BackupStatusService's 30-second debounce prevents expensive
// rescans on rapid foreground/background cycles. The cached snapshot is shown
// immediately; reconciliation only runs when truly stale.
statusService.refresh(force: false)
}
// MARK: Combine handlers

View File

@@ -1,5 +1,4 @@
import Foundation
import UserNotifications
import Photos
import os.log
@@ -11,12 +10,18 @@ final class BackupEngine: ObservableObject {
static let shared = BackupEngine()
@Published private(set) var job: BackupJob = BackupJob()
@Published private(set) var queueItems: [BackupQueueItem] = []
private let photoService: PhotoLibraryProtocol
private let transferFactory: (NASProtocol) -> any NASTransferProtocol
private var activeTransfer: (any NASTransferProtocol)?
private var isCancelled = false
private static let queueCacheURL: URL = {
let caches = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
return caches.appendingPathComponent("kisani_last_queue.json")
}()
private init(
photos: PhotoLibraryProtocol = PhotoLibraryService(),
transferFactory: @escaping (NASProtocol) -> any NASTransferProtocol = { proto in
@@ -28,6 +33,11 @@ final class BackupEngine: ObservableObject {
) {
self.photoService = photos
self.transferFactory = transferFactory
// Load last session's queue so SyncView has data to show on next launch.
if let data = try? Data(contentsOf: Self.queueCacheURL),
let items = try? JSONDecoder.kisani.decode([BackupQueueItem].self, from: data) {
self.queueItems = items
}
}
static func testInstance(
@@ -121,10 +131,19 @@ final class BackupEngine: ObservableObject {
}
// 5. Upload loop
// The main actor is released at every `await`. Progress UI is throttled
// to 8 Hz without throttling, one Task { @MainActor } is fired per
// progress callback which causes continuous SwiftUI full-tree re-renders.
job.start(totalFiles: pendingAssets.count, totalBytes: 0)
NotificationService.notifyBackupStarted(count: pendingAssets.count)
// Build initial queue (all queued) for SyncView
queueItems = pendingAssets.map { asset in
BackupQueueItem(
id: asset.localIdentifier,
filename: asset.filename,
nasPath: "\(connection.remotePath)/\(asset.filename)",
status: .queued, bytesUploaded: 0, totalBytes: 0, speedBytesPerSec: 0,
error: nil, retryCount: 0, queuedAt: Date(), completedAt: nil
)
}
var uploaded = 0
var skipped = 0
@@ -137,7 +156,7 @@ final class BackupEngine: ObservableObject {
let spUpload = signposter.beginInterval("UploadLoop", "\(pendingAssets.count) files")
for asset in pendingAssets {
for (assetIdx, asset) in pendingAssets.enumerated() {
if isCancelled { break }
while case .paused = job.status {
try await Task.sleep(nanoseconds: 500_000_000)
@@ -145,9 +164,18 @@ final class BackupEngine: ObservableObject {
let remotePath = "\(connection.remotePath)/\(asset.filename)"
// Mark as uploading
if assetIdx < queueItems.count {
queueItems[assetIdx].status = .uploading
}
if (try? await transfer.fileExists(at: remotePath)) == true {
job.fileCompleted(skipped: true)
skipped += 1
if assetIdx < queueItems.count {
queueItems[assetIdx].status = .skipped
queueItems[assetIdx].completedAt = Date()
}
logger.debug("Skipped (NAS exists): \(asset.filename, privacy: .public)")
continue
}
@@ -160,20 +188,28 @@ final class BackupEngine: ObservableObject {
let fileSize = (try? localURL.resourceValues(forKeys: [.fileSizeKey]).fileSize)
.flatMap { Int64($0) } ?? 0
// Capture totalBytes by value so the progress closure doesn't capture
// the mutable var avoids a data race warning and is semantically correct.
let bytesSoFar = totalBytes
let capturedIdx = assetIdx
if capturedIdx < queueItems.count {
queueItems[capturedIdx].totalBytes = fileSize
}
try await transfer.upload(localURL: localURL, remotePath: remotePath) { sent, _ in
let speed = speedTracker.update(bytesSent: sent)
guard throttle.shouldUpdate() else { return }
Task { @MainActor [weak self] in
self?.job.updateProgress(
guard let self else { return }
self.job.updateProgress(
fileName: asset.filename,
fileSize: fileSize,
bytesTransferred: bytesSoFar + sent,
speed: speed
)
if capturedIdx < self.queueItems.count {
self.queueItems[capturedIdx].bytesUploaded = sent
self.queueItems[capturedIdx].speedBytesPerSec = speed
}
}
}
@@ -182,6 +218,12 @@ final class BackupEngine: ObservableObject {
job.fileCompleted(skipped: false)
uploaded += 1
if assetIdx < queueItems.count {
queueItems[assetIdx].status = .uploaded
queueItems[assetIdx].bytesUploaded = fileSize
queueItems[assetIdx].completedAt = Date()
}
manifestEntries.append(ManifestEntry(
localIdentifier: asset.localIdentifier,
filename: asset.filename,
@@ -194,6 +236,15 @@ final class BackupEngine: ObservableObject {
logger.error("Upload failed for \(asset.filename, privacy: .public): \(error.localizedDescription, privacy: .public)")
job.fileFailed()
failed += 1
if assetIdx < queueItems.count {
queueItems[assetIdx].status = .failed
queueItems[assetIdx].completedAt = Date()
queueItems[assetIdx].error = BackupQueueItem.UploadError(
code: (error as NSError).domain,
message: error.localizedDescription,
timestamp: Date()
)
}
}
}
@@ -214,7 +265,23 @@ final class BackupEngine: ObservableObject {
let entry = BackupHistoryEntry(result: result, nasHost: host, triggeredByLAN: triggeredByLAN)
store.appendHistoryEntry(entry)
await sendCompletionNotification(result: result)
// Notifications
if result.failedCount > 0 {
NotificationService.notifyBackupFailed(count: result.failedCount)
} else {
NotificationService.notifyBackupCompleted(
uploaded: result.uploadedCount,
total: BackupStatusService.shared.snapshot.phoneTotal
)
}
// Persist queue so SyncView shows last session on next launch
let snapshot = queueItems
Task.detached(priority: .utility) { [snapshot, url = Self.queueCacheURL] in
guard let data = try? JSONEncoder.kisani.encode(snapshot) else { return }
try? data.write(to: url, options: .atomic)
}
return result
}
@@ -235,14 +302,6 @@ final class BackupEngine: ObservableObject {
func resolveSuccess() { job.resolveSuccess() }
private func sendCompletionNotification(result: BackupResult) async {
let content = UNMutableNotificationContent()
content.title = result.hasErrors ? "Backup completed with errors" : "Backup complete"
content.body = "\(result.uploadedCount) uploaded · \(result.skippedCount) skipped · \(result.failedCount) errors"
content.sound = .default
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
try? await UNUserNotificationCenter.current().add(request)
}
}
// MARK: SpeedTracker

View File

@@ -129,9 +129,35 @@ final class BackupStatusService: NSObject, ObservableObject {
let store = ConnectionStore.shared
guard let conn = store.savedConnection else { return }
// Step 1: Phone count
// PHFetchResult.count is O(1) after the initial index build fast on main actor.
let filter = store.backupFilter
// Fast path: use local caches when both are warm
// Skips NAS connection + PHFetchResult batch enumeration entirely.
// Triggered when: manifest cache < 5 min old AND LocalPhotoIndex has data.
let manifestCacheIsStale = await NASManifestCache.shared.isStale
let localIndexCount = await LocalPhotoIndex.shared.totalCount
if !manifestCacheIsStale, let cached = await NASManifestCache.shared.manifest, localIndexCount > 0 {
let index = await Task.detached(priority: .utility) {
ManifestIndex(manifest: cached)
}.value
let safe = await LocalPhotoIndex.shared.countSafe(against: index, filter: filter)
let phoneTotal = await LocalPhotoIndex.shared.totalCount
var updated = BackupStatusSnapshot()
updated.phoneTotal = phoneTotal
updated.alreadySafe = min(safe, phoneTotal)
updated.nasArchiveTotal = index.totalCount
updated.connectionState = .connected
updated.lastCheckedAt = Date()
snapshot = updated
saveSnapshot()
log.info("Reconcile (cached): \(phoneTotal) phone, \(safe) safe, \(index.totalCount) NAS")
return
}
// Full path: connect to NAS and download fresh manifest
// Step 1: Phone count (O(1) PHFetchResult.count)
let fetchResult = photoService.fetchResultForReconciliation(filter: filter)
let phoneTotal = fetchResult.count
log.info("PHFetchResult count: \(phoneTotal)")
@@ -159,27 +185,29 @@ final class BackupStatusService: NSObject, ObservableObject {
try Task.checkCancellation()
// Step 3: Load ManifestIndex
// Download suspends main actor (good). JSON decode + Set<String> construction
// is CPU-bound O(N) runs in a background task so touch/animation remain smooth.
let spManifest = signposter.beginInterval("ManifestLoad")
let manifestPath = "\(conn.remotePath)/\(BackupManifest.remoteFilename)"
let index = await buildManifestIndex(transfer: transfer, path: manifestPath, conn: conn)
signposter.endInterval("ManifestLoad", spManifest,
"\(index.totalCount) entries, filename-only=\(index.isFilenameOnly)")
log.info("Manifest index built — \(index.totalCount) NAS entries, filename-only=\(index.isFilenameOnly)")
log.info("Manifest index built — \(index.totalCount) NAS entries")
try Task.checkCancellation()
// Step 4: Count safe assets off main actor
// Batch enumeration of PHFetchResult is CPU-bound (up to N/150 batches).
// Running it in a detached task keeps the main thread free for the UI
// while the count is in progress. Task.yield() inside countSafe lets other
// async work interleave between batches.
// Step 4: Count safe assets
// Prefer LocalPhotoIndex (pure in-memory, no CoreData) if populated;
// otherwise fall back to batch PHFetchResult enumeration.
let spCount = signposter.beginInterval("CountSafe")
let safe = try await Task.detached(priority: .userInitiated) { [weak self, fetchResult, index] in
guard let self else { throw CancellationError() }
return try await self.countSafe(in: fetchResult, against: index)
}.value
let localCount = await LocalPhotoIndex.shared.totalCount
let safe: Int
if localCount > 0 {
safe = await LocalPhotoIndex.shared.countSafe(against: index, filter: filter)
} else {
safe = try await Task.detached(priority: .userInitiated) { [weak self, fetchResult, index] in
guard let self else { throw CancellationError() }
return try await self.countSafe(in: fetchResult, against: index)
}.value
}
signposter.endInterval("CountSafe", spCount, "\(safe)/\(phoneTotal) safe")
log.info("Comparison complete — \(safe)/\(phoneTotal) already safe")
@@ -212,6 +240,8 @@ final class BackupStatusService: NSObject, ObservableObject {
}.value
if let (index, manifest) = decoded {
lastManifest = manifest
// Persist to local cache so future launches skip the NAS round-trip.
Task { await NASManifestCache.shared.update(manifest) }
return index
}
} catch {
@@ -346,6 +376,20 @@ final class BackupStatusService: NSObject, ObservableObject {
extension BackupStatusService: PHPhotoLibraryChangeObserver {
nonisolated func photoLibraryDidChange(_ changeInstance: PHChange) {
// Update LocalPhotoIndex incrementally avoids full rebuild on every change.
// We need the fetch result for change details; rebuild from scratch if unavailable.
Task(priority: .utility) {
let opts = PHFetchOptions()
let allAssets = PHAsset.fetchAssets(with: opts)
if let details = changeInstance.changeDetails(for: allAssets) {
await LocalPhotoIndex.shared.apply(
inserted: details.insertedObjects,
removed: details.removedObjects,
changed: details.changedObjects
)
await LocalPhotoIndex.shared.flush()
}
}
Task { @MainActor [weak self] in
guard let self else { return }
self.debounceTask?.cancel()

View File

@@ -0,0 +1,163 @@
import Foundation
import Photos
import os.log
private let log = Logger(subsystem: "com.albert.nasbackup", category: "LocalPhotoIndex")
/// On-device index of phone asset metadata eliminates full PHFetchResult
/// enumeration for status counts after the initial build.
///
/// Build cost: O(N) on first launch, then zero on every subsequent open.
/// Update cost: O(changed) via PHPhotoLibraryChangeObserver.
/// Memory: ~100 bytes × asset count (1 MB for 10 k photos).
actor LocalPhotoIndex {
static let shared = LocalPhotoIndex()
struct Record: Codable, Sendable {
let localIdentifier: String
var filename: String
var creationDate: Date?
var mediaType: Int // PHAssetMediaType.rawValue
var isScreenshot: Bool
var isRAW: Bool
}
private var records: [String: Record] = [:]
private let diskURL: URL
private var needsSave = false
private init() {
let caches = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
diskURL = caches.appendingPathComponent("kisani_local_index.json")
}
// MARK: Lifecycle
/// Load from disk if available; otherwise build from PHFetchResult in background.
/// Safe to call multiple times subsequent calls are no-ops if records are loaded.
func loadOrBuild() async {
guard records.isEmpty else { return }
if let loaded = await Self.loadFromDisk(url: diskURL) {
records = loaded
log.info("LocalPhotoIndex: loaded \(self.records.count) records from disk")
return
}
log.info("LocalPhotoIndex: building from scratch (first launch)")
await buildFromLibrary()
}
// MARK: Queries
var totalCount: Int { records.count }
func allRecords() -> [Record] { Array(records.values) }
func countSafe(against index: ManifestIndex, filter: BackupFilter) -> Int {
var safe = 0
for record in records.values {
guard passes(record, filter: filter) else { continue }
if index.matches(localIdentifier: record.localIdentifier) {
safe += 1
} else if index.isFilenameOnly && index.matches(filename: record.filename) {
safe += 1
}
}
return safe
}
/// Returns localIdentifiers for assets not yet in the manifest (pending upload).
func pendingIDs(against index: ManifestIndex, filter: BackupFilter) -> [String] {
records.values.compactMap { record in
guard passes(record, filter: filter) else { return nil }
let alreadyBacked =
index.matches(localIdentifier: record.localIdentifier) ||
(index.isFilenameOnly && index.matches(filename: record.filename))
return alreadyBacked ? nil : record.localIdentifier
}
}
// MARK: Incremental updates (from PHPhotoLibraryChangeObserver)
func apply(inserted: [PHAsset], removed: [PHAsset], changed: [PHAsset]) {
for asset in removed { records.removeValue(forKey: asset.localIdentifier) }
for asset in inserted + changed {
records[asset.localIdentifier] = Self.makeRecord(asset)
}
needsSave = true
log.debug("LocalPhotoIndex: +\(inserted.count) -\(removed.count) ~\(changed.count)")
}
func flush() async {
guard needsSave else { return }
needsSave = false
let snapshot = records
let url = diskURL
await Task.detached(priority: .utility) {
guard let data = try? JSONEncoder.kisani.encode(snapshot) else { return }
try? data.write(to: url, options: .atomic)
}.value
log.debug("LocalPhotoIndex: flushed \(snapshot.count) records")
}
// MARK: Private helpers
private func passes(_ record: Record, filter: BackupFilter) -> Bool {
let img = PHAssetMediaType.image.rawValue
let vid = PHAssetMediaType.video.rawValue
if !filter.includePhotos && record.mediaType == img { return false }
if !filter.includeVideos && record.mediaType == vid { return false }
if !filter.includeScreenshots && record.isScreenshot { return false }
if !filter.includeRAW && record.isRAW { return false }
return true
}
private func buildFromLibrary() async {
let opts = PHFetchOptions()
opts.includeAssetSourceTypes = [.typeUserLibrary, .typeCloudShared, .typeiTunesSynced]
let built: [String: Record] = await Task.detached(priority: .userInitiated) {
let result = PHAsset.fetchAssets(with: opts)
var map = [String: Record](minimumCapacity: result.count)
let batchSize = 200
var i = 0
while i < result.count {
autoreleasepool {
let end = min(i + batchSize, result.count)
for j in i..<end {
let asset = result.object(at: j)
let rec = Self.makeRecord(asset)
map[rec.localIdentifier] = rec
}
i = end
}
}
return map
}.value
records = built
needsSave = true
await flush()
log.info("LocalPhotoIndex: built \(self.records.count) records")
}
private nonisolated static func loadFromDisk(url: URL) async -> [String: Record]? {
await Task.detached(priority: .utility) {
guard let data = try? Data(contentsOf: url) else { return nil }
return try? JSONDecoder.kisani.decode([String: Record].self, from: data)
}.value
}
nonisolated static func makeRecord(_ asset: PHAsset) -> Record {
let resources = PHAssetResource.assetResources(for: asset)
let filename = resources.first?.originalFilename
?? "\(asset.localIdentifier.prefix(8)).jpg"
let isRAW = resources.contains { $0.type == .alternatePhoto }
let isScreenshot = asset.mediaSubtypes.contains(.photoScreenshot)
return Record(
localIdentifier: asset.localIdentifier,
filename: filename,
creationDate: asset.creationDate,
mediaType: asset.mediaType.rawValue,
isScreenshot: isScreenshot,
isRAW: isRAW
)
}
}

View File

@@ -0,0 +1,72 @@
import Foundation
import os.log
private let log = Logger(subsystem: "com.albert.nasbackup", category: "ManifestCache")
/// Persists a copy of the NAS backup manifest on-device so reconciliation can
/// skip the NAS network round-trip when the cache is still fresh.
///
/// Cache is updated after every successful manifest download and after every
/// completed backup. Stale threshold = 5 minutes. The actor serialises all
/// reads and writes so the cache is safe to use across tasks and threads.
actor NASManifestCache {
static let shared = NASManifestCache()
private var cached: CachedEntry?
private let diskURL: URL
private static let staleInterval: TimeInterval = 5 * 60
private struct CachedEntry: Codable {
var manifest: BackupManifest
var savedAt: Date
}
private init() {
let caches = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
diskURL = caches.appendingPathComponent("kisani_manifest_cache.json")
// Load from disk immediately so the first `manifest` access is instant.
if let data = try? Data(contentsOf: diskURL),
let entry = try? JSONDecoder.kisani.decode(CachedEntry.self, from: data) {
cached = entry
log.info("ManifestCache: loaded \(entry.manifest.entries.count) entries from disk")
}
}
/// Returns the cached manifest without any I/O. Never nil after first reconcile.
var manifest: BackupManifest? { cached?.manifest }
/// True when the cache is absent or older than the stale threshold.
var isStale: Bool {
guard let c = cached else { return true }
return Date().timeIntervalSince(c.savedAt) > NASManifestCache.staleInterval
}
/// Persist a freshly-downloaded manifest. Call after every successful NAS download.
func update(_ manifest: BackupManifest) {
let entry = CachedEntry(manifest: manifest, savedAt: Date())
cached = entry
log.info("ManifestCache: updated — \(manifest.entries.count) entries")
Task.detached(priority: .utility) { [entry, url = diskURL] in
guard let data = try? JSONEncoder.kisani.encode(entry) else { return }
try? data.write(to: url, options: .atomic)
}
}
}
// MARK: Shared encoder/decoder
extension JSONEncoder {
static let kisani: JSONEncoder = {
let e = JSONEncoder()
e.dateEncodingStrategy = .iso8601
return e
}()
}
extension JSONDecoder {
static let kisani: JSONDecoder = {
let d = JSONDecoder()
d.dateDecodingStrategy = .iso8601
return d
}()
}

View File

@@ -0,0 +1,65 @@
import UserNotifications
import Foundation
/// Centralised local-notification sender.
///
/// Uses notification identifiers as categories posting the same id replaces
/// any previous notification in that category, preventing stale banners from
/// stacking up.
enum NotificationService {
static func notifyBackupStarted(count: Int) {
guard count > 0 else { return }
post(
id: "kisani.backup.started",
title: "Backup started",
body: "Kisani is backing up \(count) new \(count == 1 ? "item" : "items")."
)
}
static func notifyBackupCompleted(uploaded: Int, total: Int) {
post(
id: "kisani.backup.completed",
title: "Backup complete",
body: total > 0
? "All \(total) photos are safe."
: "\(uploaded) \(uploaded == 1 ? "item" : "items") backed up."
)
}
static func notifyBackupPaused(pendingCount: Int) {
post(
id: "kisani.backup.paused",
title: "Backup paused",
body: pendingCount > 0
? "Kisani will continue \(pendingCount) \(pendingCount == 1 ? "item" : "items") when your NAS is reachable."
: "Kisani will continue when your NAS is reachable."
)
}
static func notifyBackupFailed(count: Int) {
post(
id: "kisani.backup.failed",
title: "Backup failed",
body: "\(count) \(count == 1 ? "item" : "items") could not be backed up. Tap to review."
)
}
static func notifyNASOffline() {
post(
id: "kisani.nas.offline",
title: "NAS unreachable",
body: "Kisani will resume backup when your NAS is back online."
)
}
// MARK: Private
private static func post(id: String, title: String, body: String) {
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = .default
let request = UNNotificationRequest(identifier: id, content: content, trigger: nil)
UNUserNotificationCenter.current().add(request) { _ in }
}
}