3 분마다 위치를 업데이트하고 싶습니다. 백그라운드 작업은 처음 3 분 후에 작동하지만 이후에는 다시 시작하지 않습니다. 나는 반복적으로 일어날 필요가있다. 당신이 전화를 한 후Swift에서 백그라운드 작업이 다시 시작되지 않음
override func viewDidLoad()
{
super.viewDidLoad()
timer.invalidate()
timer = Timer.scheduledTimer(timeInterval: 179, target: self, selector: #selector(someBackgroundTask), userInfo: nil, repeats: true);
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
if CLLocationManager.locationServicesEnabled()
{
if status == CLAuthorizationStatus.notDetermined
{
locationManager.requestAlwaysAuthorization()
}
}else {
print("locationServices disenabled")
}
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
mapView.delegate = self
registerBackgroundTask()
}
func someBackgroundTask(timer:Timer) {
DispatchQueue.global(qos: .background).async {
self.locationManager.startUpdatingLocation()
timer.invalidate()
timer.fire()
self.registerBackgroundTask()
}
}
func registerBackgroundTask() {
backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
self?.someBackgroundTask(timer: (self?.timer)!)
self?.endBackgroundTask()
}
assert(backgroundTask != UIBackgroundTaskInvalid)
}
func endBackgroundTask() {
print("Background task ended.")
UIApplication.shared.endBackgroundTask(backgroundTask)
backgroundTask = UIBackgroundTaskInvalid
}
당신은 백그라운드에서 시간 일정에서 작업을 실행할 수 없습니다. 사용자가 이동할 때 업데이트를 수신하기 위해 백그라운드 위치 업데이트를 활성화하거나 정확도가 낮고 배터리 영향이 적고 중요한 위치 업데이트를 사용해야하는 경우 – Paulw11