Search code examples
swiftbackgrounddispatch

How to stop backgroundUpdateTask?


Here is code which I execute in background when user close the app, but it is weird behavior , after endBackgroundUpdateTask() method is executed , DispatchQueue still doesn't stops...

I steel continuos get notification.

What am I doing wrong?

you can try to take this snipped of code and try for yourself, it is really weird

var backgroundUpdateTask: UIBackgroundTaskIdentifier!

func beginBackgroundUpdateTask() {
    print("beginBackgroundUpdateTask")
    self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
        self.endBackgroundUpdateTask()
    })
}

func endBackgroundUpdateTask() {
    print("endBackgroundUpdateTask")
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
}

func doBackgroundTask() {
    print("Strart")
    DispatchQueue.global().async {
        self.beginBackgroundUpdateTask()

        // Do something with the result.
        let timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(AppDelegate.displayAlert), userInfo: nil, repeats: true)
        RunLoop.current.add(timer, forMode: RunLoopMode.defaultRunLoopMode)
        RunLoop.current.run()

        // End the background task.
        self.endBackgroundUpdateTask()
    }

    print("Finish")
}

func displayAlert() {
    print("displayAlert")
    let note = UILocalNotification()
    note.alertBody = "As a test I'm hoping this will run in the background every X number of seconds..."
    note.soundName = UILocalNotificationDefaultSoundName
    UIApplication.shared.scheduleLocalNotification(note)
}

func applicationDidEnterBackground(_ application: UIApplication) {
    log.debug("applicationDidEnterBackground")
    self.doBackgroundTask()

}

edit

var backgroundUpdateTask: UIBackgroundTaskIdentifier!
var timerr: Timer?

func beginBackgroundUpdateTask() {
        appDeligate.log.debug("beginBackgroundUpdateTask")
        self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
        self.endBackgroundUpdateTask()
    })
}

func endBackgroundUpdateTask() {
    appDeligate.log.debug("endBackgroundUpdateTask")
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
    timerr = nil
}

func doBackgroundTask() {
    print("Strart")
    DispatchQueue.global().async {
        self.beginBackgroundUpdateTask()

        // Do something with the result.
        self.timerr = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(TestViewController.displayAlert), userInfo: nil, repeats: true)
        RunLoop.current.add(self.timerr!, forMode: RunLoopMode.defaultRunLoopMode)
        RunLoop.current.run()

        // End the background task.
        self.endBackgroundUpdateTask()
    }

    print("Finish")
}

func displayAlert() {
    print("displayAlert")
    let note = UILocalNotification()
    note.alertBody = "As a test I'm hoping this will run in the background every X number of seconds..."
    note.soundName = UILocalNotificationDefaultSoundName
    UIApplication.shared.scheduleLocalNotification(note)
}

Solution

  • Really issue was with Timer, I was needed put this line

    timerr?.invalidate()
    

    here

    func endBackgroundUpdateTask() {
        appDeligate.log.debug("endBackgroundUpdateTask")
        UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
        self.backgroundUpdateTask = UIBackgroundTaskInvalid
        timerr?.invalidate()
    }
    

    But anyway it is little bit weird , because I thought if I was stopping backgroundUpdate() all tasks inside had to invalidate and purge automatically, but no.

    Thanks @matt