import UIKit import BackgroundTasks /// App delegate that schedules the background refresh task which drives the /// push-equivalent notifications (polling the monitored repos). /// /// iOS runs BGAppRefreshTask opportunistically (~every 15 min at best, often /// slower), and only when the app has been used recently and isn't force-quit. final class AppDelegate: NSObject, UIApplicationDelegate { static let taskIdentifier = "com.bueso.giteapr.refresh" func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool { registerBackgroundTask() return true } private func registerBackgroundTask() { BGTaskScheduler.shared.register( forTaskWithIdentifier: Self.taskIdentifier, using: nil ) { task in // Handle the background refresh. self.handle(task) } } private func handle(_ task: BGTask) { // Reschedule the next refresh so the chain continues. scheduleNextRefresh() let pollTask = Task { @MainActor in await NotificationService.shared.poll() } task.expirationHandler = { pollTask.cancel() } Task { await pollTask.value task.setTaskCompleted(success: true) } } /// Schedule the next background refresh (~15 min from now, earliest). func scheduleNextRefresh() { let request = BGAppRefreshTaskRequest(identifier: Self.taskIdentifier) request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) try? BGTaskScheduler.shared.submit(request) } }