title: Long running background task is not working properly in iOS 13
body: <p>I have a long-running background task to find similar images in a device. I have implemented code with operation queue and assign it QoS to background. Sometimes it works perfectly and sometimes it does not.</p>
  
  <pre><code>photoScanningOperationQueue.name = "photo_scanning"
  photoScanningOperationQueue.maxConcurrentOperationCount = 1
  photoScanningOperationQueue.qualityOfService = .background
  photoScanningOperationQueue.underlyingQueue = .global(qos: .background)
  </code></pre>
  
  <p>In the simulator it does not suspend the app completely, it just stops the process and gives this error but in the real device, it just stops the app and when launch from start when loaded from background.</p>
  
  <p><code>assertion's invalidation time is in the past</code></p>
  
  <p>I have used code below to achieve long-running background task </p>
  
  <pre><code>extension DuplicatePhotoFinderVC {
      func setupBackgrounding() {
          NotificationCenter.default.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
          NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
          NotificationCenter.default.addObserver(self, selector: #selector(appBackgrounding(_:)), name: UIApplication.didEnterBackgroundNotification, object: nil)
          NotificationCenter.default.addObserver(self, selector: #selector(appForegrounding(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)
      }
  
      @objc func appBackgrounding(_ notification: Notification?) {
          keepAlive()
      }
  
      @objc func keepAlive() {
          timer?.invalidate()
          timer = nil
          backgroundTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
              UIApplication.shared.endBackgroundTask(self.backgroundTask)
              self.backgroundTask = UIBackgroundTaskIdentifier.invalid
              if self.photoScanningOperationQueue.operationCount &gt; 0 {
                  self.keepAlive()
              }
          })
      }
  }
  </code></pre>
  
  <p>In the viewDidLoad method, I have called setupBackgrounding(). 
  I'm not sure what is causing this issue. I have tried to debug for 2 days and I can't find a root cause or solution to my problem.
  If you can spot a problem and give solution it'll be much appreciated.</p>