AFAICT there’s no specific restriction on scheduling an app refresh task from your applicationDidEnterBackground(_:)
delegate callback. I tried this today with a small test app and didn’t see anything weird.
Specifically, in my test app I have code like this in my app delegate:
let log = Logger(subsystem: "com.example.apple-samplecode.Test780407", category: "app-refresh")
func register() {
log.log("will register")
let ok = BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.test-task-id", using: .main) { t in
self.log.log("task did run")
}
if ok {
log.log("did register")
} else {
log.log("did not register")
}
}
var isScheduled = false
func schedule(source: String) {
do {
if self.isScheduled {
self.cancel()
}
log.log("will schedule, source: \(source, privacy: .public)")
let request = BGAppRefreshTaskRequest(identifier: "com.example.test-task-id")
try BGTaskScheduler.shared.submit(request)
log.log("did schedule")
self.isScheduled = true
} catch {
log.log("did not schedule, error: \(error)")
}
}
func cancel() {
log.log("will cancel")
BGTaskScheduler.shared.cancel(taskRequestWithIdentifier: "com.example.test-task-id")
self.isScheduled = false
log.log("did cancel")
}
I then added:
-
A call to register()
in my applicationDidFinishLaunching(_:)
.
-
Two buttons, Schedule and Cancel, wired up to the corresponding methods.
Using this I was able to test that the schedule and cancel functionality works in general.
Next I added this code:
func applicationDidEnterBackground(_ application: UIApplication) {
self.schedule(source: "did enter background")
}
On moving the app to the background, I see that method run and successfully schedule the task.
Oh, and I ran this test both with the app run from Xcode and the app run from the Home screen, just in case there’s something weird about the Xcode environment.
It sounds like there’s been some recent thrash in your main app. Given that, I recommend that you start with a new test project that focuses on just this facility. You can crib my code above to get you started. I suspect you’ll find that things work there, after which it’s time to compare your main app to your test app to see what’s different.
Alternatively, if you can reproduce the exception with the test app, that’d be quite interesting. In that case I suggest you reply back here with the details, including a full crash report. See Posting a Crash Report for info on how to attach that.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"