0

I'm doing file transfers using the Dropbox API v2. The methods include closures where the success of the transfer is available, but the method I've written to execute the transfer always returns before the closures complete and I can never set a property. The transfers work fine, I simply want to download three separate files and then put up an alert to tell the user of success.

I've looked through a bunch of similar questions on SO but so far no one has a solution. Is it not possible to capture a value in the closure and pass it to another class?

I've tried setting external properties, static properties and using intermediate functions. Nothing works. The method is:

func downloadFromDropbox(downloadFileName : String) -> Bool {
    var success = false

    if let client = DropboxClientsManager.authorizedClient {
    //file and directory stuff here

         client.files.download(path: dropFileName, overwrite: true, destination: destination)
            .response { response, error in
                if let response = response {
                    print(response)
                    success = true
                    print("success in the block: \(success)")//this prints correctly

                } else if let error = error {
                    print(error)
                    success = false
                }//if let response else
            }//.response block
            .progress { progressData in
                //print(progressData)
            }//.progress

    } else {

    //login stuff here

    }//if let client else

    print("and success before the return: \(success)") //this does not work-always false
    return success //this does not work-always false

}//downloadFromDropbox

Any guidance would be appreciated.Swift 3, IOS 10, Xcode 8.0

JohnSF
  • 3,736
  • 3
  • 36
  • 72
  • There are countless examples of this issue. The duplicate I linked happens to address the issue while using Alamofire buts it's exactly the same issue you have here. You can't use a return value for a function that does asynchronous processing. Use the linked duplicate as a simple guide for refactoring your code. – rmaddy Oct 12 '16 at 00:24
  • I get it now. Thanks. For others who may face this issue this SO session seems to be a simpler example: http://stackoverflow.com/questions/30401439/how-could-i-create-a-function-with-a-completion-handler-in-swift – JohnSF Oct 13 '16 at 22:06

0 Answers0