I've configured background task in plist.info with an identifier updateCountry and Background Modes fetch and processing as capability.
I have locationManager with these functions:
func registerBackgroundTask() {
BGTaskScheduler.shared.register(forTaskWithIdentifier: "updateCountry", using: nil) { task in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
print("register backgoundtask")
}
}
func handleAppRefresh(task: BGAppRefreshTask) {
print("registrando pais")
scheduleBackgroundTask()
DispatchQueue.global().async {
self.detectCountry()
task.setTaskCompleted(success: true)
print("Background task completed.")
}
}
func scheduleBackgroundTask() {
print("request updateCountry")
let request = BGAppRefreshTaskRequest(identifier: "updateCountry")
request.earliestBeginDate = Date(timeIntervalSinceNow: 60 * 1) // 1 minute
do {
try BGTaskScheduler.shared.submit(request)
print("Background task scheduled successfully.")
} catch {
print("Unable to submit task: \(error)")
}
}
I'm calling this functions in this way where handleAppRefresh calls a class function to detect the country where you are and add to an array... Everything seems fine, and I have no error, but nothing is printed in the console and doesn't work or update country list...
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(container)
.backgroundTask(.appRefresh("updateCountry")) {
await locationManager.registerBackgroundTask()
}
}
Anyone knows how to fix it or where is the problem?