简体   繁体   中英

Swift URLSession DataTask Fails when the app enters the background

According to Apple :

Note You don't have to do all background network activity with background sessions as described in this article. Apps that declare appropriate background modes can use default URL sessions and data tasks, just as if they were in the foreground.

I'm trying to use my DataTask with default session configuration and delegates (not completion handler) but my data task is always failing if I press the home button and switch back to the app again:

Task <A25361F9-CAC0-4FA8-8663-777E1C6878A2>.<2> load failed with error Error Domain=NSPOSIXErrorDomain Code=53 "Software caused connection abort" UserInfo={_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <A25361F9-CAC0-4FA8-8663-777E1C6878A2>.<2>, _kCFStreamErrorDomainKey=1, NSErrorPeerAddressKey=<CFData 0x108f07b40 [0x1db6c1420]>{length = 16, capacity = 16, bytes = 0x100201bb68118e240000000000000000}, _kCFStreamErrorCodeKey=53, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <A25361F9-CAC0-4FA8-8663-777E1C6878A2>.<2>"

What I tried: using the shared session with or without completion handler, same problem.

My question is: what does this mean "Apps that declare appropriate background modes can use default URL sessions and data tasks"? How do you declare those background modes??

The only thing I came across is UIApplication.shared.beginBackgroundTask

Is this what Apple means by "Appropriate background modes"? Or Am I missing something?

thank you

Yes, beginBackgroundTask(withName:expirationHandler:) is the correct way to request the OS to give your app a little time to finish requests it started before the user left the app. See Extending Your App's Background Execution Time .

Is this what Apple means by “Appropriate background modes”?

They're talking about any of the techniques that might have your app running in the background. See About the Background Execution Sequence . And a “background task”, in which you're given a finite amount of time to finish a task even though your app is no longer in the foreground, is one of those modes.

Another is background fetch . If enabled, during background fetch, the OS may, at its sole discretion, fire up your app in the background, let you perform a request, and you then call the completion handler when it's done. (In that case, you have even less than 3 minutes, something more like 30 seconds IIRC.) That is an example of an “appropriate background mode” for which you'd use a standard/default URLSessionConfiguration , not a background one. Apple makes a point of this because it would otherwise be easy to assume that any background-related network requests would require background URLSessionConfiguration . But that's not the case.

So, if you're just trying to let the app keep running a bit to finish the task even if the user hit the home button, then beginBackgroundTask in conjunction with a standard URLSession is adequate. Or if you happened to be using one of those “approved background modes” (which doesn't sound like would be the situation in your case), then again, a standard URLSession is adequate.

A background URLSession is really for those cases where request may take longer than the allotted time. Perhaps you're downloading many very large assets, such as a movies, that might take more than a few minutes. Only in that case do you need to use a background URLSessionConfiguration.background(withIdentifier:) . It just depends upon how long the request(s) will take.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM