Last active
July 3, 2019 14:55
-
-
Save mecid/be571a8a27cc70ab425ad0627cc645b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import BackgroundTasks | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
BGTaskScheduler.shared.register( | |
forTaskWithIdentifier: "pl.snowdog.example.refresh", | |
using: DispatchQueue.global() | |
) { task in | |
self.handleAppRefresh(task) | |
} | |
return true | |
} | |
private func handleAppRefresh(_ task: BGTask) { | |
let queue = OperationQueue() | |
queue.maxConcurrentOperationCount = 1 | |
let appRefreshOperation = AppRefreshOperation() | |
queue.addOperation(appRefreshOperation) | |
task.expirationHandler = { | |
queue.cancelAllOperations() | |
} | |
let lastOperation = queue.operations.last | |
lastOperation?.completionBlock = { | |
task.setTaskCompleted(success: !(lastOperation?.isCancelled ?? false)) | |
} | |
scheduleAppRefresh() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment