Using Start Background Task with Timeout Handler to Load

From the doc, it looks like uploading a file is a good use case begin​Background​Task​With​Expiration​Handler​

. I found that using

let uploadTask = session.uploadTask(with: request as URLRequest, fromFile: file)
uploadTask.resume()

      

will be executed while the application is running (I get a progress message for a while). Also, I can set the URLSession to run in the background:

let config = URLSessionConfiguration.background(withIdentifier: "uploads")
session = URLSession(configuration: config, delegate: self, delegateQueue: nil)

      

What is the advantage of using it begin​Background​Task​With​Expiration​Handler

? Will this extend the time that I have to finish downloading? If so, can I know how much (didn't see anything about this in the doc)? Or is it just that I will ping before the application stops? Should I use it in conjunction with a background url?

Also, the docs state that the handler will be called shortly before the remaining usage time of the apps reaches 0. Does that mean the app will be terminated after that? those. is it possible to assume that the next call will be application:didFinishLaunchingWithOptions

or maybe applicationDidBecomeActive

?

+3


source to share


2 answers


This background task will allow your application to continue running in the background after the user leaves your application for an additional 3 minutes or so (check background​Time​Remaining

for the actual value) for your request to complete. And, yes, at the end of those 3 minutes, the timeout handler will be called if you haven't finished the background job yet.

So, if you end a background job during the normal thread of your application, this timeout closure won't need to be called. This shutdown is solely for any quick last-minute cleanup that might be required before the app stops running in the background because it was shut down before you had a chance to indicate that the background task has finished. This is not to start something new, but just one last final clean-up. And don't forget to finish the background job in that timeout handler ... if you don't finish the background job, the OS will kill your application, not just suspend it. Often, the only thing you need to do in this timeout is the background task completes, but if you need to do any other cleanup, you can.



Needless to say, you must complete your background job (either when the network request completes, or in a timeout handler if your app hasn't yet gotten a chance to finish the background job on a normal thread). If you don't, your app won't just be suspended, but rather killed.

As far as assumptions about what happens when your application is restarted by the user later, you cannot make any assumptions about which application delegation method will be called. Even if you gracefully finished a background job, you have no assurance that it won't be dropped for other reasons (like memory pressure). So don't assume anything.

+2


source


What is the advantage of using an initial background job with an expiration handler?



If you are going to use URLSessionConfiguration.background

, there is no such advantage and you shouldn't use beginBackgroundTask(expirationHandler:)

at all. Your whole premise (your first sentence) was wrong. File upload is not a good option for beginBackgroundTask(expirationHandler:)

. This is a good use case for URLSessionConfiguration.background

. These two things have nothing to do with each other.

+1


source







All Articles