title: How to stop backgroundUpdateTask?
body: <p>Here is code which I execute in background when user close the app, but it is weird behavior , after <code>endBackgroundUpdateTask()</code> method is executed , <code>DispatchQueue</code> still doesn't stops...</p>
  
  <p>I steel continuos get notification.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>you can try to take this snipped of code and try for yourself, it is really weird</p>
  
  <pre><code>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()
  
  }
  </code></pre>
  
  <p><strong>edit</strong></p>
  
  <pre><code>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)
  }
  </code></pre>