cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to know what we learned at IBC? Check out our learnings on media, remote working and more right here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Downloading in the background in iOS (Swift)

Downloading in the background in iOS (Swift)

VyacheslavBakinskiy
Helpful | Level 6
Go to solution

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()

                }

        }

    }

1 Accepted Solution

Accepted Solutions

VyacheslavBakinskiy
Helpful | Level 6
Go to solution

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.

View solution in original post

11 Replies 11

Greg-DB
Dropbox Staff
Go to solution

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?

VyacheslavBakinskiy
Helpful | Level 6
Go to solution

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-DB
Dropbox Staff
Go to solution

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?

VyacheslavBakinskiy
Helpful | Level 6
Go to solution

I leave the application in the background for a few seconds. I also tried leaving it on for a few minutes. The result is the same. I'm using iOS 14.

Greg-DB
Dropbox Staff
Go to solution

Thanks! That's the same as my test setup though, so I'm still not sure why you're seeing a different behavior.

 

I see you included a line for defining your client object ("private let client..."), but where exactly are you running that? The issue may be related to the lifecycle of that object. If it's no longer available, the operation may not complete. If you try using DropboxClientsManager.authorizedClient directly, does that change the behavior?

 

Likewise, is it possible your app is just getting terminated entirely when its in the background? You may want to add some logging to see what's happening in the lifecycle of the app.

VyacheslavBakinskiy
Helpful | Level 6
Go to solution

I tried to use DropboxClientsManager.authorizedClient?.files.download(...) dicectly but it doesn't solve issue. I sure that my app doesn't getting terminated entirely because I also downloading from Google Drive in the similar conditions and it works fine. 

The downloadFile function is a function of the Protocol under which classes for working with Dropbox and Google Drive are signed, and if the problem was in these classes or functions, then I'd have the same problem with downloading from Google Drive.

Suggest me some other solutions, pls, I'm desperate.

Greg-DB
Dropbox Staff
Go to solution

Ok, in that case we'll have to look into it further.

 

I do still recommend adding some more logging though. The comparison with Google Drive may not be exactly representative since that may use a different mechanism than the Dropbox SDK.

VyacheslavBakinskiy
Helpful | Level 6
Go to solution

Of course I'll look at my code and xcode log again, but I'll also wait for a decision from your side in this topic.
PS Another reason I'm sure my app doesn't shut down in the background is because it keeps playing music.

VyacheslavBakinskiy
Helpful | Level 6
Go to solution

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.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    VyacheslavBakinskiy Helpful | Level 6
What do Dropbox user levels mean?