05/08 Updated code: fix some issues 3
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// JQCApp.swift
|
||||
// ------------
|
||||
// App entry point. Phase C adds BGTaskScheduler for background sync.
|
||||
// App entry point.
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
@@ -23,11 +23,9 @@ struct JanitorialQCApp: App {
|
||||
.environmentObject(sync)
|
||||
}
|
||||
.modelContainer(for: [
|
||||
// Phase A
|
||||
LocalFacility.self,
|
||||
LocalArea.self,
|
||||
LocalTemplate.self,
|
||||
// Phase B
|
||||
LocalInspection.self,
|
||||
LocalIssue.self,
|
||||
PendingPhoto.self,
|
||||
@@ -35,24 +33,17 @@ struct JanitorialQCApp: App {
|
||||
], isUndoEnabled: false) { result in
|
||||
switch result {
|
||||
case .success(let container):
|
||||
Task { @MainActor in
|
||||
SyncManager.shared.modelContext = container.mainContext
|
||||
await AuthManager.shared.restoreSession()
|
||||
SyncManager.shared.startMonitoring()
|
||||
if SyncManager.shared.isOnline {
|
||||
await SyncManager.shared.triggerSync()
|
||||
}
|
||||
}
|
||||
// Only set the model context here — do NOT await anything.
|
||||
// Session restore and sync are triggered by ContentView.task{}
|
||||
// which runs on the MainActor inside the SwiftUI lifecycle,
|
||||
// guaranteeing isLoading changes are seen by the view immediately.
|
||||
SyncManager.shared.modelContext = container.mainContext
|
||||
case .failure(let error):
|
||||
fatalError("SwiftData container failed: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Background Tasks ──────────────────────────────────────────────────
|
||||
|
||||
/// Register the background sync task with the OS.
|
||||
/// The identifier must match BGTaskSchedulerPermittedIdentifiers in Info.plist.
|
||||
private func registerBackgroundTasks() {
|
||||
BGTaskScheduler.shared.register(
|
||||
forTaskWithIdentifier: "com.jqc.sync",
|
||||
@@ -67,18 +58,13 @@ struct JanitorialQCApp: App {
|
||||
}
|
||||
|
||||
private func handleBackgroundSync(task: BGProcessingTask) {
|
||||
// Schedule the next background run immediately
|
||||
scheduleBackgroundSync()
|
||||
|
||||
let syncTask = Task {
|
||||
await SyncManager.shared.triggerSync()
|
||||
}
|
||||
|
||||
// If the OS needs to cancel early, cancel our work
|
||||
task.expirationHandler = {
|
||||
syncTask.cancel()
|
||||
}
|
||||
|
||||
Task {
|
||||
await syncTask.value
|
||||
task.setTaskCompleted(success: !syncTask.isCancelled)
|
||||
@@ -86,8 +72,6 @@ struct JanitorialQCApp: App {
|
||||
}
|
||||
}
|
||||
|
||||
/// Schedule a background processing task.
|
||||
/// Call this from sceneDidEnterBackground or after each foreground sync.
|
||||
func scheduleBackgroundSync() {
|
||||
let request = BGProcessingTaskRequest(identifier: "com.jqc.sync")
|
||||
request.requiresNetworkConnectivity = true
|
||||
|
||||
Reference in New Issue
Block a user