在完成处理程序之前创建进度块的正确语法

时间:2016-09-16 04:35:35

标签: ios swift

我正在创建一个单独的类来处理我的Amazone S3上传请求。但是,我不太确定允许我在完成处理程序之前创建进度块的语法(如下面的IBAction所示)。基本上我希望实现的是在我的VC中,我执行以下操作:

@IBAction startUpload() {
    let uploadPost = PostUpload(imageNSData: someNSData)() 

    uploadPost.uploadBegin { 
        // Some block here to grab the "progress_in_percentage" variable so I can use it on progress bar 
        {
            // Some completion block when the request is completed and check if any error was returned
        }
    }
}

这是PostUpload类的结构

class PostUpload {

    var imageNSData: NSData!

    init(imageNSData: NSData) {
        self.imageNSData = imageNSData
    }

    func uploadBegin(completion:(success: Bool, error: NSError?) -> Void) {

        // 1. Create upload request
        let uploadRequest = AWSS3TransferManagerUploadRequest(


        // Track progress through an AWSNetworkingUploadProgressBlock
        uploadRequest?.uploadProgress = {[weak self](bytesSent:Int64, totalBytesSent:Int64, totalBytesExpectedToSend:Int64) in
            dispatch_sync(dispatch_get_main_queue(), { () -> Void in

                let progress_in_percentage = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
                print(progress_in_percentage)
            })
        }

        // 3. Upload to Amazone S3
        let transferManager = AWSS3TransferManager.defaultS3TransferManager()
        transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task: AWSTask) -> AnyObject? in

            if let error = task.error {
                completion(true, error)

            } else {
                completion(true, nil)
            }
            return nil
        })

    }
}

2 个答案:

答案 0 :(得分:4)

将您的方法更改为:

(Int) => Any

用法:

func uploadBegin(progressUpdate: ((percent: Float) -> Void)? = nil, completion:(success: Bool, error: NSError?) -> Void) {

        // 1. Create upload request
        let uploadRequest = AWSS3TransferManagerUploadRequest(


            // Track progress through an AWSNetworkingUploadProgressBlock
            uploadRequest?.uploadProgress = {[weak self](bytesSent:Int64, totalBytesSent:Int64, totalBytesExpectedToSend:Int64) in
                dispatch_sync(dispatch_get_main_queue(), { () -> Void in

                    let progress_in_percentage = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
                    print(progress_in_percentage)

                    //Call this to update progress
                    progressUpdate?(progress_in_percentage)
                })
        }

        // 3. Upload to Amazone S3
        let transferManager = AWSS3TransferManager.defaultS3TransferManager()
        transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task: AWSTask) -> AnyObject? in

            if let error = task.error {
                completion(true, error)

            } else {
                completion(true, nil)
            }
            return nil
        })

    }

或者:

uploadBegin { (success, error) in
            //completion block
        }

答案 1 :(得分:1)

你可以在函数参数中添加多个闭包,在你的情况下只需添加进度回调闭包作为函数的第一个参数

func uploadBegin(uploadProgress:(percentage: Float) -> Void, completion:(success: Bool, error: NSError?) -> Void) {

    // 1. Create upload request
    let uploadRequest = AWSS3TransferManagerUploadRequest(

        // Track progress through an AWSNetworkingUploadProgressBlock
        uploadRequest?.uploadProgress = {[weak self](bytesSent:Int64, totalBytesSent:Int64, totalBytesExpectedToSend:Int64) in
            dispatch_sync(dispatch_get_main_queue(), { () -> Void in

                let progress_in_percentage = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
                // report progress here
                uploadProgress(percentage: progress_in_percentage)
                print(progress_in_percentage)
            })
    }

    // 3. Upload to Amazone S3
    let transferManager = AWSS3TransferManager.defaultS3TransferManager()
    transferManager.upload(uploadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (task: AWSTask) -> AnyObject? in

        if let error = task.error {
            completion(true, error)

        } else {
            completion(true, nil)
        }
        return nil
    })

}

使用

uploadPost.uploadBegin({ (percentage) in
    // progress handle here
}) { (success, error) in
    // completion block
}