您好,我已经在plist.info中配置了后台任务,并将其标识符为"updateCountry",并将后台模式作为获取和处理功能. 我有具有此功能的LocationManager...
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)")
}
}
我以这种方式调用这些函数,其中,AppAppRefresh调用一个类函数来检测您所在的国家并添加到数组中.一切似乎都很好,我没有错误,但没有什么是在控制台打印,不工作或更新国家列表.
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(container)
.backgroundTask(.appRefresh("updateCountry")) {
await locationManager.registerBackgroundTask()
}
}
有谁知道如何解决这个问题,或者问题出在哪里?谢谢!