GCD's primary goal is maximum TTL

I need to be able to program background tasks. Small "crowns", if you want, execute some simple code. Although I was not an expert in GCD, I was wondering:

  • What is the maximum time I expect the background task to actually fulfill its responsibilities in the background before the applications complete completely.
  • Can I “program” several tasks and expect them to be completed in time.
  • Are they active only when the application starts? (I’m sure that they, unlike local notifications, which really don’t care if the application is running in the background or not, so just be sure to be sure)
  • How can I track them and cancel them if necessary?

    For example, I can do something like this and complete a task. I did 1 minute here and it works.

    let backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: nil)
    DispatchQueue.main.asyncAfter(wallDeadline: DispatchWallTime.now() + 60) {
                // Some action here
                UIApplication.shared.endBackgroundTask(backgroundTaskIdentifier)
    })
    
+4
source share
1 answer

You can print the remaining time for this session using backgroundTimeRemaining(docs here ). Apple makes no guarantees what will happen this time, it depends on the battery level, hardware, resources, etc. Therefore, this is probably not suitable for a long-term constant background task. You might want to consider the background fetch API , although it is similarly throttled by iOS and you do not have full control over when it starts.

0
source

Source: https://habr.com/ru/post/1673835/


All Articles