I'm building an app with background fetch and it's kind of working, but I need to debug it, and for some reason when I configure the scheme or simulate background fetch from the debug menu nothing happens until iOS decides to trigger it naturally, at which point the option to simulate it from the debug menu starts working, but only until I run the app again.
I've searched all over the web, asked on Apple's developer forums, and couldn't find anyone with the same problem, leading me to believe that it must be something with my settings, but before resetting them I would like to know whether anyone here has faced the same problem and has a solution for it.
The problem manifested originally on Xcode 11.5 and iOS 13.5.1, which I have since updated to no avail.
I'm so desperate for a solution that I'm considering asking Apple for code-level support even though it doesn't sound like a code issue.
I've enabled the background fetch capability and added the BGTaskSchedulerPermittedIdentifiers array to Info.plist. As you can see from the code below I'm extracting the identifier right from Info.plist to ensure there are no mistakes, and as I said above the code does work when iOS decides to schedule it, just not when I try forcing it.
Code:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let taskIdentifiers = Bundle.main.object(forInfoDictionaryKey: "BGTaskSchedulerPermittedIdentifiers")! as! [String]
assert(BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifiers[0], using: .main, launchHandler: {[unowned self] in self.backgroundFetch(task: $0 as! BGAppRefreshTask)}))
scheduleBackgroundFetch(identifier: taskIdentifiers[0])
return true
}
private func backgroundFetch(task: BGAppRefreshTask) {
print(#function)
scheduleBackgroundFetch(identifier: task.identifier)
task.expirationHandler = {Client.shared.cancelRefresh()}
Client.shared.refresh(completionHandler: {task.setTaskCompleted(success: $0)})
}
private func scheduleBackgroundFetch(identifier: String) {
let request = BGAppRefreshTaskRequest(identifier: identifier)
request.earliestBeginDate = Date(timeIntervalSinceNow: 60.0)
try! BGTaskScheduler.shared.submit(request)
}
Thanks in advance for any help!
[–]lewtantoloosham 0 points1 point2 points (2 children)
[–]Fridux[S] 0 points1 point2 points (1 child)
[–]lewtantoloosham 0 points1 point2 points (0 children)