Skip to content

Instantly share code, notes, and snippets.

@eito
Created April 26, 2020 19:09
Show Gist options
  • Save eito/5840b4f47264a7651be39406fbb25cf1 to your computer and use it in GitHub Desktop.
Save eito/5840b4f47264a7651be39406fbb25cf1 to your computer and use it in GitHub Desktop.
import UIKit
import BackgroundTasks
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// app data, just contains a lastUpdateTime property that is set whenever
// handleAppRefresh is called as a way to validate the bgFetch is being triggered
// by iOS
//
let timeViewModel = TimeViewModel()
let appRefreshIdentifier = "com.esri.dev.timekeeper.refresh"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
BGTaskScheduler.shared.register(forTaskWithIdentifier: appRefreshIdentifier, using: nil) { (task) in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
}
return true
}
func scheduleAppRefresh() {
print("Scheduling app refresh...")
let request = BGAppRefreshTaskRequest(identifier: appRefreshIdentifier)
request.earliestBeginDate = Date()
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Failed to submit task: \(error)")
}
}
private func handleAppRefresh(task: BGAppRefreshTask) {
print("handling app refresh....")
scheduleAppRefresh()
// - make network request to fetch data
// - serialize data, if necessary
// - dispatch to main to update
//
DispatchQueue.main.async {
self.timeViewModel.lastUpdatedTime = Date()
task.setTaskCompleted(success: true)
}
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let timeViewModel = (UIApplication.shared.delegate as? AppDelegate)?.timeViewModel else {
return
}
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView().environmentObject(timeViewModel)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
func sceneDidEnterBackground(_ scene: UIScene) {
guard let appDelegate = (UIApplication.shared.delegate as? AppDelegate) else {
return
}
appDelegate.scheduleAppRefresh()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment