We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Mark R.5
9 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...
Greg-DB
Dropbox Community Moderator
9 years agoHi 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.59 years agoHelpful | 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’sUIApplicationDelegateobject. 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-DB9 years ago
Dropbox Community Moderator
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. - Stephen C.149 years ago
Dropbox Staff
Hi Mark, sorry for the delay here. Unfortunately, the SDK doesn't expose an interface for implementing this session re-attachment behavior.
I'm no expert in this behavior, but if I recall, after sesion reattachment, you need to resupply a response handler to handle the upload or download that completed while your app was not running.
There's no way to do that other than to handwrite some logic in `application(_:handleEventsForBackgroundURLSession:completionHandler:)`.
You'd have to maintain some sort of map between request id and response handler there, and reinstall the handler accordingly. This would complicate the SDK interface considerably.
For that reason, the SDK simply chooses not to handle the case where the application is killed.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
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, Facebook or Instagram.
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!