Your workflow is unique 👨‍💻 -  tell us how you use Dropbox here.

Forum Discussion

Ricky B.5's avatar
Ricky B.5
New member | Level 1
10 years ago

Download files from Dropbox (swift & SDK 1.3.13)

Hello,

I noticed that DBRestClient is asynchronous, it means that When I download file from Dropbox, I cannot read it immediately.
I put in place a While (to wait upload completed) but I have not success, could you please help?

Thanks
Sébastien

My VIEW code:

class ViewController: UIViewController, DBRestClientDelegate {
    private var FileLoaded = false

    ...

    @IBAction func btDownload(sender: AnyObject) {
        txtFileReader.text=""
        txtLogs.insertText("Start DL file \n")
        let tmpDirectory = NSTemporaryDirectory()
        let myFilename = tmpDirectory.stringByAppendingPathComponent("Test.txt")
        FileLoaded=false
        dbRestClient?.loadFile("/MyFolder/Test.txt", intoPath: myFilename)
        while FileLoaded {
            txtFileReader.text = String(contentsOfFile: myFilename, encoding: NSUTF8StringEncoding, error: nil)
        }
        txtLogs.insertText(" \(myFilename) \n")
        txtLogs.insertText("End DL file \n")
    }

    func restClient(client: DBRestClient!, loadedFile localPath: String, contentType contentType: String, metadata metadata: DBMetadata!){
        println("File loaded into path: \(localPath)")
        FileLoaded=true
    }
    func restClient(client: DBRestClient!, loadFileFailedWithError error: NSError!){
        println("LoadFile failed with error: \(error)")
    }

3 Replies

Replies have been turned off for this discussion
  • Just put your code to read the file in your implementation of loadedFile.

  • Ricky B.5's avatar
    Ricky B.5
    New member | Level 1
    10 years ago

    Hello Steve,
    Thanks for your reply, it exactly what I did and it works properly.
    Just for my information, there is no better way to catch the result ? like I wanted to do before ?

    Thanks again,
    Sébastien

  • This SDK uses delegates, so the expected way to write code is to call loadFile and then continue with the next step in loadedFile. If you take a look at our v2 Swift SDK preview, you'll see that we're moving towards using blocks in the future instead of delegates, which I hope you'll find more intuitive.

About Dropbox API Support & Feedback

Node avatar for 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!