1

I sometimes see code where the task is ended using

bgTaskID = UIBackgroundTaskInvalid

At other places I see

UIApplication.sharedApplication().endBackgroundTask(bgTaskID)

At some other places I see:

  UIApplication.shared.endBackgroundTask(bgTaskID)
  backgroundTask = UIBackgroundTaskInvalid

What's the difference between these three? Reading from here it seems it's just a matter of clean code, but I find that unnecessary, you've ended the task!

To me they are all the same. Only that using you UIBackgroundTaskInvalid you can validate if a task has ended or not. Not sure why you'd ever need to validate though.

Is the above assessment correct?

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • The first one doesn't end any task, you could do it after the task is ended so that you don't have a property pointing to the id of a task that is no longer valid. – dan Jul 24 '17 at 18:09
  • I wrote a blog post myself years later. Check it out here: https://mfaani.com/posts/uiapplication-backgroundtasks-through-the-lens-of-closures/ – mfaani Mar 11 '23 at 20:11

0 Answers0