1
我正在创建一个独立的类来处理我的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
})
}
}
这个答案也是正确的。较早的一个被选中。干杯 – user172902