cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
What’s new: end-to-end encryption, Replay and Dash updates. Find out more about these updates, new features and more 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: 

How to Upload Files Asynchronously

How to Upload Files Asynchronously

Vegepilot
Explorer | Level 3

I'm running into trouble uploading too many files at once.  How do I upload asynchronously.  Or even just a hint for which direction to look for answers.

 

What I'm doing is saving the same under several different names contained in the fileNumberArray.

 

 

for number in fileNumberArray {
            
                        let request = client?.files.upload(path: "/Path/(number).mp3", mode: .overwrite, autorename: false, clientModified: nil, mute: false, input: audioURL)
                            .response { response, error in
                                if let response = response {
                                    print(response)
                                } else if let error = error {
                                    print(error)
                                }
                            }
                            .progress { progressData in
                                
                                if let myProgress = progressData as? Progress {

                                    self.progressView.progress = Float(progressData.fractionCompleted)
                                    
                                }
                        }
                        
                    }
            
        })
1 Reply 1

Greg-DB
Dropbox Staff
The SwiftyDropbox library runs API requests for these methods asynchronously. That is, when you call client.files.upload, that method will return before the API request itself completes.

So, to limit how many are running at once, you need control how many times you call client.files.upload to begin with. You can't just run it in a loop like this because code execution will continue through the next iterations before the API requests for the previous calls complete.

Exactly how you manage this is up to you. You can use whatever makes sense for your app. For example, you can keep track of how many calls you've kicked off and then update that state (and kick off more if appropriate) in the response block. Alternatively, you may want to use a semaphore.
Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?