BGProcessingTaskRequest execute randomly.

I am using BGProcessingTaskRequest to fetch a API make my app up to date. Sometimes this background task execute with 10 minutes some times it take more than 10 mins, Some other times its never execute.

But in my case im provide just 1 minute to BGProcessingTaskRequest.earliestBeginDate variable. And i will share my implementation here,

My codes are,

Called the register function before app launching.

let taskId = "_________"
func registerBackgroundTaks() {
        BGTaskScheduler.shared.register(forTaskWithIdentifier: taskId, using: nil) { task in
            self.handleBackgroundProcessRequest(task: task as! BGProcessingTask)
        }
        print("Receiver called")
    }

Called the scheduleBackgroundPrecessingTask function when application enter in background mode.

func scheduleBackgroundPrecessingTask() {
        let request = BGProcessingTaskRequest(identifier: taskId)
        request.requiresNetworkConnectivity = false // Need to true if your task need to network process. Defaults to false.
        request.requiresExternalPower = false
        
        request.earliestBeginDate = Date(timeIntervalSinceNow: 1 * 60) // Featch Image Count after 1 minute.
        do {
            try BGTaskScheduler.shared.submit(request)
            print("Process notification triggered")
        } catch {
            print("Could not schedule background process: \(error)")
        }
    }

Could anyone share any concerns to my problem? or kindly clarify me why BGProcessingTaskRequest takes time randomly?

Answered by DTS Engineer in 809018022

Before you continue, please read iOS Background Execution Limits.

Sometimes this background task execute with 10 minutes some times it take more than 10 mins

That doesn’t surprise me. A background processing task (created with BGProcessingTaskRequest) is not guaranteed to run on any particular schedule. Rather, the schedule is an implementation detail. I typically see these run overnight.


This is the same issue that my app faces with iOS18

Are you talking about a processing task (BGProcessingTaskRequest) or an app refresh task (BGAppRefreshTaskRequest)?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

This is the same issue that my app faces with iOS18, although using Objective C in place of Swift. The background sync is scheduled to execute every 10 min, but sometime it executes after 1 hr and sometimes completely misses the execution.

Moreover the BG sync is also skipped when not connected to power. Looking for quick resolution !!

Before you continue, please read iOS Background Execution Limits.

Sometimes this background task execute with 10 minutes some times it take more than 10 mins

That doesn’t surprise me. A background processing task (created with BGProcessingTaskRequest) is not guaranteed to run on any particular schedule. Rather, the schedule is an implementation detail. I typically see these run overnight.


This is the same issue that my app faces with iOS18

Are you talking about a processing task (BGProcessingTaskRequest) or an app refresh task (BGAppRefreshTaskRequest)?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi @surya_s , well the background tasks management on Apple platform are in some aspects really mysterious, but I think ( @DTS Engineer confirm this) that you have no guarantees that background task could execute at specific time, you only specific that you should perform task on background, but from this time, it's a matter of the background task manager how and when execute the task itself.

Please note that the earliestBeginDate itself has following description


   /// The earliest date and time at which to run the task
    ///
    /// Specify `nil` for no start delay.
    ///
    /// Setting the property indicates that the background task shouldn’t start any
    /// earlier than this date. However, the system doesn’t guarantee launching the
    /// task at the specified date, but only that it won’t begin sooner.
    open var earliestBeginDate: Date?
}

Bye Rob

the background tasks management on Apple platform are in some aspects really mysterious

Indeed.

you have no guarantees that background task could execute at specific time

Right. I go into this more in iOS Background Execution Limits, and the WWDC talk that it references covers it in more depth [1].

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] The link in my post is broken but I’ve left the link there because it can help you find the info you need.

BGProcessingTaskRequest execute randomly.
 
 
Q