Using iOS 13.3.1 Xcode 11.3.1 Swift 5
Code says this, compiles but doesn't run correctly. When I background my app I get an error message that says "fails with no background task exists with identifier 1 or it may have already been ended".
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
BGTaskScheduler.shared.register(forTaskWithIdentifier: "ch.blah.refresh", using: nil) { (task) in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
}
return true
}
func applicationDidEnterBackground(_ application: UIApplication) {
scheduleAppRefresh()
}
var request: BGAppRefreshTaskRequest!
func scheduleAppRefresh() {
request = BGAppRefreshTaskRequest(identifier: "ch.blah.refresh")
request.earliestBeginDate = Date(timeIntervalSinceNow: 60)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
func handleAppRefresh(task: BGAppRefreshTask) {
scheduleAppRefresh()
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
queue.addOperation {
for i in 1 ... 1000000 {
print("\(i)")
}
}
let lastOp = queue.operations.last
lastOp?.completionBlock = {
task.setTaskCompleted(success: !lastOp!.isCancelled)
}
task.expirationHandler = {
queue.cancelAllOperations()
}
}
Yes, I did add the key to Info.plist
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>ch.blah.refresh</string>
</array>
I am getting several failures due to iOS 13. What am I missing?
I did try downloading the Apple WWDC code to take a look. But it has the same problem, it appears.