Just following up as I still don't quite understand.
Does RefreshAppContentsOperation run automatically in the background at the designated time if the task is BGTaskScheduler.shared.register() and BGTaskScheduler.shared.submit() ran, or is that why there is operationQueue? If I were to not use operationQueue what would be the alternative?
class BackgroundTasksController: ObservableObject {
func scheduleTask() {
BGTaskScheduler.shared.register(forTaskWithIdentifier: "app.Task", using: nil) { task in
Task {
await self.handleAppRefresh(task: task as! BGAppRefreshTask)
}
}
}
private func handleAppRefresh(task: BGAppRefreshTask) async {
Task {
scheduleAppRefresh()
RefreshAppContentsOperation()
}
}
func scheduleAppRefresh() {
let request = BGAppRefreshTaskRequest(identifier: "app.Task")
request.earliestBeginDate = Date(timeIntervalSinceNow: 60)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
}
final class RefreshAppContentsOperation: Operation, @unchecked Sendable {
}
Topic:
App & System Services
SubTopic:
Processes & Concurrency
Tags: