Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
iOS background task with all the checks and balances
class DoesSomethingInBackground
{
var backgroundTaskID: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
func beginBackgroundTask() {
guard UIBackgroundTaskInvalid == backgroundTaskID else {return}
backgroundTaskID = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
self.endBackgroundTask()
}
}
func endBackgroundTask() {
guard UIBackgroundTaskInvalid != backgroundTaskID else {return}
let id = backgroundTaskID
backgroundTaskID = UIBackgroundTaskInvalid
UIApplication.sharedApplication().endBackgroundTask(id)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.