Created
April 7, 2020 14:06
-
-
Save wata/59b8f7e7205840b6b2b6a0c08fbdf8c0 to your computer and use it in GitHub Desktop.
Avoid app termination without calling the `endBackgroundTask(_:)`.
This file contains 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 | |
final class BackgroundTask { | |
private var taskID = UIBackgroundTaskIdentifier.invalid | |
func begin(withName taskName: String = #function) { | |
taskID = UIApplication.shared.beginBackgroundTask(withName: taskName) { | |
UIApplication.shared.endBackgroundTask(self.taskID) | |
self.taskID = UIBackgroundTaskIdentifier.invalid | |
} | |
} | |
func end() { | |
guard taskID != UIBackgroundTaskIdentifier.invalid else { return } | |
UIApplication.shared.endBackgroundTask(taskID) | |
} | |
deinit { | |
end() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage