2016-09-16 8 views
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 
     }) 

    } 
} 

答えて

3
はこれにあなたの方法を変更し

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 
     } 

または:

uploadBegin({ (percent) in 
    //Update Progress on UI 
    }) { (success, error) in 
     //completion block 
} 
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 
} 
+0

この回答も正解です。以前のものが選択されました。乾杯 – user172902

関連する問題