Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Registered BGTaskScheduler task isn't running launchHandler

$
0
0

I'm trying to schedule a background task to run in the background using BGTaskScheduler.shared.register(identifier:queue:launchHandler:), but the launchHandler block is never executed.

What I've tried:

  • I have tried both on Simulator and physical device.
  • I have enabled background modes (Background fetch).
  • and have submitted the task in SceneDelegate on sceneDidEnterBackground(:) using BGTaskScheduler.shared.submit(:).
  • I have also added the identifier to "Permitted background task scheduler identifiers" in the Info.plist.
  • I have tried selecting Debug > Simulate Background Fetch.

Here's my code:

func scene(_ scene: UIScene,           willConnectTo session: UISceneSession,           options connectionOptions: UIScene.ConnectionOptions) {            let isRegistered = BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.example.bgTaskIdentifier",                                                       using: nil) { task in        print("Executed") // Does not print        self.handleAppRefresh(task: task as! BGAppRefreshTask)    }    print("isRegistered: \(isRegistered)") // Prints "isRegistered: true"}func sceneDidEnterBackground(_ scene: UIScene) {    scheduleAppRefresh()    print("Entered Background") // Prints "Entered Background"}func scheduleAppRefresh() {    let request = BGAppRefreshTaskRequest(identifier: "com.example.bgTaskIdentifier")    request.earliestBeginDate = Date(timeIntervalSinceNow: 5 * 60)    do {        try BGTaskScheduler.shared.submit(request)    } catch {        print("Could not schedule app refresh: \(error)") // Does not reach the catch block    }}

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>