|
9 | 9 | import Foundation |
10 | 10 | #else |
11 | 11 | import UIKit |
12 | | - |
13 | | - /// A protocol that defines background task handling capabilities. |
14 | | - protocol BackgroundTaskCreator: AnyObject { |
15 | | - /// Marks the beginning of a new long-running background task. |
16 | | - /// |
17 | | - /// - Parameter handler: A handler to be called shortly before the app’s remaining background time reaches 0. |
18 | | - /// You should use this handler to clean up and mark the end of the background task. Failure to end the task |
19 | | - /// explicitly will result in the termination of the app. The handler is called synchronously on the main |
20 | | - /// thread, blocking the app’s suspension momentarily while the app is notified. |
21 | | - /// - Returns: A unique identifier for the new background task. You must pass this value to the |
22 | | - /// `endBackgroundTask:` method to mark the end of this task. This method returns `UIBackgroundTaskInvalid` |
23 | | - /// if running in the background is not possible. |
24 | | - func beginBackgroundTask(expirationHandler handler: (() -> Void)?) -> UIBackgroundTaskIdentifier |
25 | | - |
26 | | - /// Marks the end of a specific long-running background task. |
27 | | - /// |
28 | | - /// You must call this method to end a task that was started using the `beginBackgroundTask(expirationHandler:)` |
29 | | - /// method. If you do not, the system may kill your app. |
30 | | - /// |
31 | | - /// This method can be safely called on a non-main thread. |
32 | | - /// |
33 | | - /// - Parameter identifier: An identifier returned by the `beginBackgroundTask(expirationHandler:)` method. |
34 | | - func endBackgroundTask(_ identifier: UIBackgroundTaskIdentifier) |
35 | | - } |
36 | | - |
37 | | - extension UIApplication: BackgroundTaskCreator {} |
38 | 12 | #endif |
39 | 13 |
|
40 | 14 | /// A class that handles background tasks to prevent iOS from suspending the app while tasks are ongoing. |
41 | 15 | class BackgroundHandler: NSObject { |
42 | 16 | #if !os(OSX) |
43 | 17 | /// The background task creator, typically `UIApplication.shared`. |
44 | | - var backgroundTaskCreator: BackgroundTaskCreator = UIApplication.shared |
| 18 | + var backgroundTaskCreator: UIApplication = UIApplication.shared |
45 | 19 |
|
46 | 20 | /// The background task identifier if a background task has started. `nil` if not. |
47 | 21 | @SynchronizedLock private var taskIdentifier: UIBackgroundTaskIdentifier? |
@@ -97,7 +71,7 @@ class BackgroundHandler: NSObject { |
97 | 71 | guard counter == 0 else { |
98 | 72 | return false |
99 | 73 | } |
100 | | - |
| 74 | + UIApplication.shared.beginBackgroundTask() |
101 | 75 | if taskIdentifier != UIBackgroundTaskIdentifier.invalid { |
102 | 76 | backgroundTaskCreator.endBackgroundTask(taskIdentifier) |
103 | 77 | } |
|
0 commit comments