We are aware of the issue with the badge emails resending to everyone, we apologise for the inconvenience - learn more here.
Forum Discussion
VyacheslavBakinskiy
5 years agoHelpful | Level 6
Downloading in the background in iOS (Swift)
Hello, I'm using file download from Dropbox in my app. It works fine when app is active, but if my app goes into the background the download stops. How can I solve it? I use this code to download:
private let client = DropboxClientsManager.authorizedClient
func downloadFile(_ file: TrackModelProtocol?, callback: @escaping () -> ()) {
guard let path = file?.path, let pathComponent = file?.fileName else { return }
if let client = client {
let fileManager = FileManager.default
let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let destURL = directoryURL.appendingPathComponent(pathComponent)
let destination: (URL, HTTPURLResponse) -> URL = { temporaryURL, response in
return destURL
}
client.files.download(path: path, overwrite: true, destination: destination)
.response { _ , error in
if let error = error{
print("Downloading from Dropbox fails: \(error)")
return
}
//Call some action after the download is complete
callback()
}
}
}
Hello, Greg!
I have new information regarding my question. On what do you run the code on a real device or on the Xcode simulator? I've always tested on a real device and was getting this issue. I decided to test it on the simulator and was very surprised that the download works fine on it. I also noticed that when I run the code on a real device - I get this issue, but if I then click to download another file, then I'll see that the first file is immediately downloaded, as if it had already been downloaded, but the completion handler didn't work.
What could be the issue? Why does it work fine on the simulator?
P.S. This issue happens only in case when device connected to the PC (Xcode), when I run app without connection to the PC the download works fine.
- Greg-DBDropbox Staff
Thanks for the report. First, can you let me know the version number of the SwiftyDropbox SDK you're using?
Also, when you say "the download stops", do you get any error or output? And, do you get any result when you return to the app?
- VyacheslavBakinskiyHelpful | Level 6
I use 6.0.3 version of SwiftyDropbox. No, I don't get any error. When I return to the app I see that download isn't complete. If you look at my code you'll see that I have completion handler which works when dowload is complete, and after return to the app I see that it didn't work.
Also in the downloadFile func I get download progress (I didn't include it in the question so as not to complicate)
.progress { progressData in
let progress = Double(progressData.fractionCompleted)
completion(progress)
print(progress)
}
When I start downloading a file and then minimize the application, then I'll see that progress stops and it doesn't start again if I return to the app
- Greg-DBDropbox Staff
Thanks for the additional information. I just tried reproducing this with the code you provided. While I also see the progress reporting stop when the app is sent to the background, if I then bring it back to the foreground the download does then resume and complete.
Can you let me know how long you're leaving the app in the background before resuming it? Also, what version(s) of iOS are you seeing this with?
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,877 PostsLatest Activity: 4 hours agoIf 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!