1

I've been trying to implement background fetch in my app for iOS 13 and 14.x Here's the steps I followed

  1. Enable Background fetch mode in the project capabilities
  2. In the info.plist, add an array for key BGTaskSchedulerPermittedIdentifiers with the relevant id for bkg fetch task
  3. In the AppDelegate didFinishLaunchingWithOptions:, register the background task
BGTaskScheduler.shared.register(forTaskWithIdentifier: bkgTaskId, using: nil) { (task) in
  self.handleAppRefreshTask(task: task as! BGAppRefreshTask)
}
  1. handleAppRefreshTask(task: BGAppRefreshTask) handles the background task. It has the expirationHandler set and marks task completed once done

  2. At the end of the above method, schedule the task to be fired after an interval of 1 hour (or whenever iOS schedules it)

    func scheduleRefresh() {
        let request = BGAppRefreshTaskRequest(identifier: bkgTaskId)
        request.earliestBeginDate = Date(timeIntervalSinceNow: 3600)
        do {
            try BGTaskScheduler.shared.submit(request)
        } catch {
            Log("Could not schedule app refresh: \(error)")
        }
    }
  1. In applicationDidEnterBackground:, call scheduleRefresh to schedule the background task

When I simulate the background fetch task from Xcode (running the app on a device), I get the below in the console and the background task is never scheduled

[ProcessSuspension] 0x28138ef40 - WKProcessAssertionBackgroundTaskManager: Ignored request to start a new background task because RunningBoard has already started the expiration timer

Anything on what I might be doing wrong or how to fix this would be very helpful. Thanks.

lostInTransit
  • 70,519
  • 61
  • 198
  • 274

0 Answers0