private func update() { // Update will be delyed for very short time (1/10 of second) to see if another update will appear right after this one. This way if both location and radius changes in the same time, update will not perform twice. let updateCheckToken = random() self.updateCheckToken = updateCheckToken let backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler(nil) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(NSEC_PER_SEC) / 10), dispatch_get_main_queue()) { [weak self] in if self != nil { if updateCheckToken == self!.updateCheckToken { self!.forceUpdateRightAway() } } // TODO: This fails in simulator. UIApplication.sharedApplication().endBackgroundTask(backgroundTaskIdentifier) } }
Nie potrafię znaleźć powodu czemu ten kod powoduje pojawienie się
Can't endBackgroundTask: no background task exists with identifier 1, or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
To jest uruchamiane w tle, spowodowane np. przez significant location update. Tworzę zadanie przez beginBackgroundTaskWithExpirationHandler, a po 1/10 sekundy je kończę, jest mi to potrzebne, żeby w ciągu tej 1/10 sekundy aplikacja nie została zawieszona, zanim wykona wszystko w dispatch_after.