I'm migrating my team's app to use NSURLSession instead NSURLConnection's because of the numerous now-deprecated APIs in the NSURLConnection family of classes and protocols. As part of that, we need to decide how often the app should be creating a new session vs. reusing sessions that exist.

Based on Apple's documentation, WWDC videos and common sense, it's not a great idea to create a new session for each new URL request. Sadly, Apple's documentation doesn't say whether there would be adverse effects of doing this w.r.t memory, CPU or something else.

Based on my application's needs, which include performing work both when the app is foregrounded and when it's backgrounded, I could in theory have only as few sessions in the app as there are different session configurations. The caveat would be that non-background sessions will probably have to be canceled when the app enters the background, and re-initialized when it enters the foreground.

Are you aware of any best practices or recommendations for when an app should create a new session vs reuse existing ones? I know the standard answer would be "it's app-specific", so maybe a better question is "when is it not advisable to create a new session"?

  • 1
    Have your read this? It provides significant guidance about when to use sessions, including specific guidance on background sessions. – Robert Harvey Mar 15 '17 at 23:01
  • 1
    @RobertHarvey Yes, I've read this a few times. What it doesn't say is how creating new sessions affects system resource usage, and whether there are any limits that developers need to be weary of. I've found through Apple developer forums that, for instance, people have experienced perf issues when their app creates more than a certain number of sessions. Apple's own example of a web browser says that each tab could have its own session, with each URL connection initiated in the tab getting its own task. In my iOS app, I could in theory use just one global session. But is that a good idea? – RuslanD Mar 15 '17 at 23:42
  • Have you tried testing the performance to see where your desired sweet spot is? If this is a performance problem, the best practice is to measure in your target environment. – Robert Harvey Mar 16 '17 at 0:01
  • 1
    @RobertHarvey the app is fairly large and talks to a lot of service endpoints, and I'm trying to migrate it to NSURLSession in increments. The type of perf investigation you mention can happen only after I've done most of that work. What I was looking for was useful pointers for the direction in which to take that work. It's understood that I may need to make further changes in the face of app-wide perf testing. – RuslanD Mar 16 '17 at 0:17
  • And "One NSURLSession Per Tab" doesn't do it for you? That's as close as any of us are going to get to a reasonable guesstimate without substantially more information about your application. – Robert Harvey Mar 16 '17 at 0:19

Use one NSURLSession per configuration. This means that most calls to a single server will go through the same session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.