-
Notifications
You must be signed in to change notification settings - Fork 0
Background Task Manager #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
76 changes: 76 additions & 0 deletions
76
Sources/MSLFoundation/BackgroundTaskManager/AsyncOperation.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import BackgroundTasks | ||
|
|
||
| /// Create another class that conforms to this class to help manage your async operation | ||
| /// https://www.avanderlee.com/swift/asynchronous-operations/ | ||
| open class AsyncOperation: Operation { | ||
| private let lockQueue: DispatchQueue | ||
|
|
||
| public init(label: String) { | ||
| self.lockQueue = DispatchQueue(label: label, attributes: .concurrent) | ||
| } | ||
|
|
||
| override public var isAsynchronous: Bool { | ||
| return true | ||
| } | ||
|
|
||
| private var _isExecuting = false | ||
| override public private(set) var isExecuting: Bool { | ||
| get { | ||
| return self.lockQueue.sync { () -> Bool in | ||
| return self._isExecuting | ||
| } | ||
| } | ||
| set { | ||
| willChangeValue(forKey: "isExecuting") | ||
| self.lockQueue.sync(flags: [.barrier]) { | ||
| self._isExecuting = newValue | ||
| } | ||
| didChangeValue(forKey: "isExecuting") | ||
| } | ||
| } | ||
|
|
||
| private var _isFinished = false | ||
| override public private(set) var isFinished: Bool { | ||
| get { | ||
| return self.lockQueue.sync { () -> Bool in | ||
| return self._isFinished | ||
| } | ||
| } | ||
| set { | ||
| willChangeValue(forKey: "isFinished") | ||
| self.lockQueue.sync(flags: [.barrier]) { | ||
| self._isFinished = newValue | ||
| } | ||
| didChangeValue(forKey: "isFinished") | ||
| } | ||
| } | ||
|
|
||
| override open func cancel() { | ||
| super.cancel() | ||
|
|
||
| self.finish() | ||
| } | ||
|
|
||
| override public func start() { | ||
| super.start() // calls main() | ||
|
|
||
| guard !self.isCancelled else { | ||
| self.finish() | ||
| return | ||
| } | ||
|
|
||
| self.isFinished = false | ||
| self.isExecuting = true | ||
| } | ||
|
|
||
| override open func main() { | ||
| fatalError("Subclasses must implement `main` without overriding super.") | ||
| } | ||
|
|
||
| public func finish() { | ||
| guard self.isExecuting else { return } | ||
|
|
||
| self.isExecuting = false | ||
| self.isFinished = true | ||
| } | ||
| } |
155 changes: 155 additions & 0 deletions
155
Sources/MSLFoundation/BackgroundTaskManager/BackgroundTaskManager.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import BackgroundTasks | ||
| import Combine | ||
| import Foundation | ||
| import Logging | ||
| import UIKit | ||
|
|
||
| private let logger: Logger = { | ||
| var logger = Logger(label: "\(#file)") | ||
| logger.logLevel = .info | ||
| return logger | ||
| }() | ||
|
|
||
| public enum EnqueueType { | ||
| /// Replace the existing task with the provided one | ||
| case replace | ||
|
|
||
| /// Keep the existing task instead of using the provided one | ||
| case keep | ||
| } | ||
|
|
||
| public final class BackgroundTaskManager { | ||
| private let taskIdentifier: String | ||
|
|
||
| private let queue = OperationQueueManager() | ||
|
|
||
| /// The current background refresh task that woke up the application | ||
| private var backgroundTask: BGAppRefreshTask? | ||
|
|
||
| /// Create a new BackgroundTaskManager with a unique identifier. This unique identifier will be used when tasks are executed in the background. | ||
| public init( | ||
| taskId: String | ||
| ) { | ||
| self.taskIdentifier = taskId | ||
|
|
||
| BGTaskScheduler.shared.register( | ||
| forTaskWithIdentifier: self.taskIdentifier, | ||
| using: nil | ||
| ) { task in | ||
| guard let task = task as? BGAppRefreshTask else { return } | ||
| self.handleBackgroundTask(task) | ||
| } | ||
|
|
||
| // Observe the app entering the foreground | ||
| NotificationCenter.default.addObserver( | ||
| self, | ||
| selector: #selector(self.handleActivate(_:)), | ||
| name: UIScene.willEnterForegroundNotification, | ||
| object: nil | ||
| ) | ||
|
|
||
| // Observe the app entering the background | ||
| NotificationCenter.default.addObserver( | ||
| self, | ||
| selector: #selector(self.handleDeactivate(_:)), | ||
| name: UIScene.willDeactivateNotification, | ||
| object: nil | ||
| ) | ||
|
|
||
| self.queue.addListener(self) | ||
| } | ||
|
|
||
| deinit { | ||
| self.queue.removeListener(self) | ||
| } | ||
| } | ||
|
|
||
| // MARK: Public Functions | ||
|
|
||
| public extension BackgroundTaskManager { | ||
| /// Adds a new task to the manager. EnqueueType can be used to either `keep` or `replace` a provider that | ||
| /// has already been register with the same `identifier`. | ||
| func register(type: EnqueueType = .keep, provider: OperationWorkProvider) { | ||
| self.queue.register(type: type, provider: provider) | ||
| } | ||
|
|
||
| /// Removes a task from the background manager. | ||
| func unregister(provider: OperationWorkProvider) { | ||
| self.queue.unregister(provider: provider) | ||
| } | ||
|
|
||
| /// Begin runing registered tasks. | ||
| func start() { | ||
| self.queue.start() | ||
| } | ||
|
|
||
| /// Prevent the BackgroundTaskManager from running any tasks. | ||
| func stop() { | ||
| self.queue.stop() | ||
| } | ||
| } | ||
|
|
||
| // MARK: Helpers | ||
|
|
||
| extension BackgroundTaskManager { | ||
| @objc private func handleActivate(_ notification: Notification) { | ||
| BGTaskScheduler.shared.cancel( | ||
| taskRequestWithIdentifier: self.taskIdentifier | ||
| ) | ||
| } | ||
|
|
||
| @objc private func handleDeactivate(_ notification: Notification) { | ||
| self.scheduleBackgroundTasks() | ||
| } | ||
|
|
||
| private func scheduleBackgroundTasks() { | ||
| let backgroundTask = BGAppRefreshTaskRequest(identifier: self.taskIdentifier) | ||
| backgroundTask.earliestBeginDate = self.queue.nextRunDate | ||
|
|
||
| do { | ||
| try BGTaskScheduler.shared.submit(backgroundTask) | ||
|
|
||
| BGTaskScheduler.shared.getPendingTaskRequests { tasks in | ||
| let details = tasks.map(\.description).joined(separator: "\n") | ||
| logger.debug("\(tasks.count) background tasks scheduled:\n\(details)") | ||
| } | ||
| } catch { | ||
| logger.error("Failed to schedule background tasks!") | ||
| logger.error("\(error.localizedDescription)") | ||
| } | ||
| } | ||
|
|
||
| private func handleBackgroundTask(_ task: BGAppRefreshTask) { | ||
| defer { | ||
| // Schedule the next background task | ||
| self.scheduleBackgroundTasks() | ||
| } | ||
|
|
||
| logger.info("App woke up for background refresh task: \(task.description)") | ||
|
|
||
| self.backgroundTask = task | ||
|
|
||
| self.queue.start() | ||
|
|
||
| task.expirationHandler = { | ||
| logger.info("Background refresh task expired") | ||
|
|
||
| self.queue.stop() | ||
|
|
||
| self.backgroundTask = nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension BackgroundTaskManager: QueueManagerListener { | ||
| func didCompleteQueue() { | ||
| logger.info("Background task completed 1 round of work") | ||
| } | ||
|
|
||
| func didSleepQueue() { | ||
| logger.info("Background work did finish") | ||
|
|
||
| self.backgroundTask?.setTaskCompleted(success: true) | ||
| self.backgroundTask = nil | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.