Sunday, November 8, 2026
HomeiOS Developmentios - BGAppRefreshTask not executing SwiftUI

ios – BGAppRefreshTask not executing SwiftUI

[ad_1]

I am getting inconsistent outcomes when utilizing the Background Duties framework for my utility written in SwiftUI. I am solely trying to make fast community requests, so I am selecting to make use of BGAppRefreshTask.

Background fetch, and Background Processing are set in Signing & Capabilities. Permitted background process scheduler identifiers have been set. Manually calling it in debugging works positive on an actual gadget however by no means in manufacturing.

I examined each BGAppRefreshTask, and BGProcessingTask. I observed BGProcessingTask is being referred to as however solely when linked to an influence provide. I by no means see any updates from BGAppRefreshTask. I am undecided if I am lacking one thing easy.

BGAppRefreshTask hasn’t run for two days.BGProcessingTask was run 13 time’s in a single day however provided that my gadget is charging. Even when setting requiresExternalPower to false.

BGAppRefreshTask run: 0 & BGProcessingTask run: 13

BGAppRefresh

/*!
 BGAppRefreshTask
 @summary A background process used to replace your app's contents within the background.
 */
class BGADelegate: NSObject,  UIApplicationDelegate, ObservableObject {
    let taskIdentifier = "com.bgapp.refresh"
    @AppStorage("backgroundtask") var duties: Int = 0
    
    func utility(_ applicatiown: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        register()
        scheduleAppRefresh()
        return true
    }

    func register() {
        BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, utilizing: nil) { process in
            self.handleAppRefresh(process: process as! BGAppRefreshTask)
            print("register")
        }
    }
    
    func scheduleAppRefresh() {
        let request = BGAppRefreshTaskRequest(identifier: taskIdentifier)
        request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
        do {
            attempt BGTaskScheduler.shared.submit(request)
        } catch {
            print("Couldn't schedule app refresh: (error)")
        }
    }
    
    func handleAppRefresh(process: BGAppRefreshTask) {
        process.expirationHandler = {
            process.setTaskCompleted(success: false)
        }
        DispatchQueue.important.async {
            self.duties += 1
        }
        // Community request right here
        process.setTaskCompleted(success: true)
        print("deal with app refresh")
    }
    
}

BGProcessingTask

/*!
 BGProcessingTask
 @summary A background process used to carry out deferrable processing.
 */
class BGPDelegate: NSObject,  UIApplicationDelegate, ObservableObject {
    let taskIdentifier = "com.bgapp.refresh"
    @AppStorage("backgroundtask") var duties: Int = 0
    
    func utility(_ applicatiown: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        register()
        scheduleAppRefresh()
        return true
    }

    func register() {
        BGTaskScheduler.shared.register(forTaskWithIdentifier: taskIdentifier, utilizing: nil) { process in
            self.handleAppRefresh(process: process as! BGProcessingTask)
            print("register")
        }
    }
    
    func scheduleAppRefresh() {
        let request = BGProcessingTaskRequest(identifier: taskIdentifier)
        request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
        request.requiresNetworkConnectivity = true
        request.requiresExternalPower = false // Default worth in false
        do {
            attempt BGTaskScheduler.shared.submit(request)
        } catch {
            print("Couldn't schedule app refresh: (error)")
        }
    }
    
    func handleAppRefresh(process: BGProcessingTask) {
        scheduleAppRefresh()
        process.expirationHandler = {
            process.setTaskCompleted(success: false)
        }
        DispatchQueue.important.async {
            self.backgroundtask += 1
        }
        // Community request right here
        process.setTaskCompleted(success: true)
        print("deal with app refresh")
    }
    
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments