Forum Discussion

Vegepilot's avatar
Vegepilot
Explorer | Level 3
8 years ago

How to Upload Files Asynchronously

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)
                                    
                                }
                        }
                        
                    }
            
        })
  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox 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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.5,942 PostsLatest Activity: 3 hours ago
351 Following

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!