Pixel Code
Pixel Code

Reputation: 89

How to Extending 'backgroundTimeRemaining' more time?

I wanna extend backgroundTimeRemaining more than 30 seconds and according to Apple

"The value is valid only after the app enters the background and has started at least one task using beginBackgroundTask(expirationHandler:) in the foreground.

System conditions may end background execution earlier, either by calling the expiration handler, or by terminating the app."

so I try to add and edit but it can't work

here's what I tried

//MARK:- BeginBackgroundTask
func registerBackgroundTask() {
  backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
    self?.endBackgroundTask()
    print(self!.beginTime)
  }

//TODO: Add new background time ex: 60 sec
var backgroundTimeRemaining: TimeInterval {
        get{
            return 60
        }
    }

assert(backgroundTask != .invalid)
} 

//MARK:- EndBackgroundTask
func endBackgroundTask() {
  print("Background task ended.")
  UIApplication.shared.endBackgroundTask(backgroundTask)
  backgroundTask = .invalid
}

Upvotes: 5

Views: 4600

Answers (2)

ChengEn
ChengEn

Reputation: 107

You can't specify any background running time for your app, instead you choose when your app would be awake by system/ notification, check Choosing Background Strategies for Your App

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299355

backgroundTimeRemaining is informational to your app. The app does not control how much time the system gives it. You can request some background time in order to finish up some user-requested action, and you may receive it, but you don't have any control over how much time. You will need to redesign to not require this.

The point of beginBackgroundTask is to mark finite-length activities that, if the app were to go into the background in the middle, it would be useful to get a few extra seconds to finish up. If, for example, you are starting background tasks in willEnterBackground, or you are not calling a balancing endBackgroundTask in a timely manner, you are probably misusing the system and the system will tend to not give you background time at all.

See Advances in App Background Execution for Apple's latest guidance on background execution.

Upvotes: 2

Related Questions