One month down in 2025: How are your resolutions coming along? Check out how to get back on track here.
Forum Discussion
Mark R.5
8 years agoHelpful | Level 5
Background Session for NSURLSession in iOS
I am after a little advice on background/foreground session usage. The documentation states that by default, background sessions are used for uploads and download. As I understand it, this means that the transfer can complete even if the app is no longer running. You can optionally use foreground sessions instead, but then you lose the ability to transfer when the app is suspended or quit.
In my case, I am only uploading/downloading small files (as part of a data syncing strategy). This is mainly done in the foreground when the user adds data to the app. If the user comes out of the app, then the app asks for more time to complete any data transfers (using the beginBackgroundTaskWithName app delegate method). This is typically sufficient for the small size of the data involved.
So would I be better off setting sessions to run in the foreground? The only downside I can see if that if the app is quit or the network connection is lost, then the transfers will not complete. However the syncing code is designed to check for incomplete transfers and try again when the app is restarted, which should mitigate against this.
I am a little wary of background transfers since I am not sure how it behaves if the transfer is completed when the app is not running (and therefore my syncing code it not there to control it)...
- Greg-DB
Dropbox Staff
Hi Mark, the SDK does give you the choice of using foreground or background sessions, and it sounds like you have a good understanding of the difference already. This is a bit outside the scope of Dropbox API support though, so I'm afraid I can't offer much insight myself. Hopefully there's someone with more experience with iOS development that can offer some advice. (Or, you may want to ask in a more iOS-specific forum, such as Apple's forums.)- Mark R.5Helpful | Level 5
Hi Greg,
I think part of my query does actually relate directly to the framework. If you read the 'Background Transfer Considerations' section in Apple's documentation:
then it explains what an app should do in the event that a transfer completes when the app is not running. Specifically:
----------------------------------
In iOS, when a background transfer completes or requires credentials, if your app is no longer running, iOS automatically relaunches your app in the background and calls the
application:handleEventsForBackgroundURLSession:completionHandler:
method on your app’sUIApplicationDelegate
object. This call provides the identifier of the session that caused your app to be launched. Your app should store that completion handler, create a background configuration object with the same identifier, and create a session with that configuration object. The new session is automatically reassociated with ongoing background activity. Later, when the session finishes the last background download task, it sends the session delegate aURLSessionDidFinishEventsForBackgroundURLSession:
message. In that delegate method, call the previously stored completion handler on the main thread so that the operating system knows that it is safe to suspend your app again.In both iOS and OS X, when the user relaunches your app, your app should immediately create background configuration objects with the same identifiers as any sessions that had outstanding tasks when your app was last running, then create a session for each of those configuration objects. These new sessions are similarly automatically reassociated with ongoing background activity.
------------------------------------
Since the Dropbox framework manages the sessions, then does it also deal with the fiddling around described above or is that left up to the app?
- Greg-DB
Dropbox Staff
I'm checking in with the team to get some information on the specifics of this in the Dropbox SDK. Thanks in advance for your patience.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,945 PostsLatest Activity: 36 minutes ago
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!