嵌入函数返回值后,在函数中调用completionHandler?迅速

时间:2016-09-29 00:37:59

标签: swift function completionhandler

在包含其他函数的函数中调用completionHandler()的正确方法是什么?基本上,我想等到嵌入式函数完成后再调用completionHandler。

func somefunc {    

transferManager?.download(downloadRequest).continue( {(task: AWSTask) -> AnyObject! in

    // Your handler code here
    if (task.error != nil) {
        print("- Error while downloading!")
        print(task.error)
    }

    else if (task.result != nil) {
        //let downloadOutput: AWSS3TransferManagerDownloadOutput = task.result as! AWSS3TransferManagerDownloadOutput
        do {
            let dFile  = try NSString(contentsOf: downloadingFileURL as URL, encoding: String.Encoding.utf8.rawValue)
            print(dFile)
            modelsParseCSV(contentsOfURL: downloadingFileURL)
        }
        catch {
            print("- Error: Unable to retrieve contents of csv file")
        }
    }
    else {
        print("- Uknown error: AWSS3 get file")
    }


    print("------------ AWS Get Models File End ----------")
    return nil

})

completionHandler()
}

1 个答案:

答案 0 :(得分:0)

我能够解决自己的问题。以下代码调用完成处理程序并在嵌入函数完成后调用:

func AWS_getModelsFile(AWSMake: String, completionHandler: (() -> Void)! ) {

    .........

    transferManager?.download(downloadRequest).continue( {(task: AWSTask) -> AnyObject! in

        // Your handler code here
        if (task.error != nil) {
            print("- Error while downloading!")
            print(task.error)
        }

        else if (task.result != nil) {
            //let downloadOutput: AWSS3TransferManagerDownloadOutput = task.result as! AWSS3TransferManagerDownloadOutput
            do {
                let dFile  = try NSString(contentsOf: downloadingFileURL as URL, encoding: String.Encoding.utf8.rawValue)
                print(dFile)
                modelsParseCSV(contentsOfURL: downloadingFileURL)
            }
            catch {
                print("- Error: Unable to retrieve contents of csv file")
            }
        }
        else {
            print("- Uknown error: AWSS3 get file")
        }


        print("------------ AWS Get Models File End ----------")
        completionHandler()
        return nil

    })
}