ios – Updating the URLSessionConfiguration of a background URLSession


How does one replace the configuration of a background session?

Taking a look at Apple’s documentation on URLSessionConfiguration
https://developer.apple.com/documentation/basis/urlsessionconfiguration

it says:

You will need to configure your URLSessionConfiguration object
appropriately earlier than utilizing it to initialize a session object. Session
objects make a duplicate of the configuration settings you present and use
these settings to configure the session. As soon as configured, the session
object ignores any modifications you make to the URLSessionConfiguration
object. If it is advisable to modify your switch insurance policies, you should replace
the session configuration object and use it to create a brand new URLSession
object.

I additionally collect a static identifier is finest, because it helps re-associate any operating duties ought to the app be terminated. I’ve seen in some posts individuals have tried to create a UUID within the index and persist that: How you can revalidate the session as soon as it’s invalidated Alamofire? however that feels fairly fragile and cumbersome.

In my app, I need to change whether or not the configuration permits downloading over mobile. Listed below are my steps:

  1. I cancel all activity – (I hold a file of issues I need to obtain afterward)
  2. I name invalidateAndCancel()
  3. I recreate the session by passing the up to date configuration.
  4. I attempt to create new duties from the listing in level 1.

Nonetheless, this throws an error:

Process created in a session that has been invalidated

Thanks prematurely in your assist!

Leave a Reply