Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing allowsCellularAccess on existing NSURLSession

Is it possible to change the value for allowsCellularAccess on an existing NSURLSession by modifying the underlying NSURLSessionConfiguration?

I want to honor any changes in a user's settings for my application without cancelling existing requests if their device is currently connected to WiFi.

like image 616
JustinHK Avatar asked Mar 13 '23 01:03

JustinHK


1 Answers

No. A session copies its configuration. It does not retain it. What I would do in your situation is:

  • Make a copy of the session's existing configuration and change that flag.
  • Create a new session with the modified configuration.
  • If the user is on Wi-Fi, call finishTasksAndInvalidate on the old session. This will keep the session around long enough to finish any existing requests, after which it will go away.
  • If the user is on cellular, call invalidateAndCancel, then wait to restart those tasks until the user is on Wi-Fi.

Additionally, you may be able to call cancelByProducingResumeData: on a task, and then recreate (resume) it in a different session with a different configuration. The task will still report its original configuration for allowsCellularAccess, but will behave according to the configuration of the new session. (The stale reporting might be considered a bug.)

like image 170
dgatwood Avatar answered Mar 24 '23 18:03

dgatwood