【问题标题】:Swift upload multiple files parallel into AWS S3 and show progress view status in tableview cellSwift 将多个文件并行上传到 AWS S3 并在 tableview 单元格中显示进度视图状态
【发布时间】:2019-01-21 04:27:54
【问题描述】:

我是 AWS 新手,我已经使用 TransferUtility 文件转换将一些文件上传到 AWS S3。这是我的场景步骤

1.从 iCloud 中挑选文件

public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

        let fileurl: URL = url as URL
        let filename = url.lastPathComponent
        let file-extension = url.pathExtension
        let filedata = url.dataRepresentation

        // Call upload function
        upload(file: fileurl, keyname: filename, exten: file-extension)

        // Append names into array
        items.append(item(title: filename, size: string))
        self.tableView_util.reloadData()

2。使用 transfer-utility 将该文件上传到 AWS S3

private func upload(file url: URL, keyname : String, exten: String) {
 transferUtility.uploadfile(file ur,
        bucket: "YourBucket",
        key: "YourFileName",
        contentType: "text/plain",
        expression: expression,
        completionHandler: completionHandler).continueWith {
           (task) -> AnyObject! in
               if let error = task.error {
                  print("Error: \(error.localizedDescription)")
               }

               if let _ = task.result {
                  // Do something with uploadTask.
               }
               return nil;
       }

3.上传时需要在tableview单元格中显示每个文件的上传状态

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cellutil", for: indexPath) as! UtilityTableViewCell
        let item = items[indexPath.row]
}

我的问题:我可以显示上传项目的表格视图,但是当我上传下一个时,第一次上传停止。我需要实现并行上传多个文件并显示单元状态。

【问题讨论】:

  • 据我了解,您是否要将多个文件上传到 s3 并显示进度?
  • 是的。@ Samarth Kejriwal ....我想将多个文件上传到 s3 并在 tableview 单元格中显示进度。我已经完成了上传过程,但我被困在 tableview 单元格显示中。文件 A 上传时间如果我上传文件 B 然后文件 A 停止。我认为我在 tableview 数据重新加载或其他方面犯了错误
  • 意思是要在s3上同时上传多个文件,并显示对应单元格的上传进度?
  • 看看你能做的是在服务器上上传一个文件,然后在 s3 服务器响应的失败或成功回调时上传下一个文件,如果成功则从你的数组中删除上传的文件并上传下一个,如果失败,请不要从数组中删除该文件并继续上传下一个文件。这样做直到你的数组变空。
  • 对于并行上传,您需要多个传输实用程序实例,目前您似乎有一个传输实用程序实例,它可以暂停上一次上传并开始新的上传

标签: ios swift amazon-web-services amazon-s3 awss3transferutility


【解决方案1】:

为此,您创建一个操作队列,每个上传文件在操作内部写入网络请求并将这些操作添加到队列中。

我在这里暗示要这样做。

创建一个具有类似属性的模型类

struct UploadRecordData { 
    let fileName:String
    let unique_id:String
    let progress:double
    //...etc
}

然后是这样的子类操作

    struct UploadRecordOperation:Operation{
        let uploadRecordData:UploadRecordData
        //etc..

        //update progess inside of operation class
        func updateProgress(progress:Double){
            uploadRecordData.progress = progress
            //edited answer
            let myDict = [ "progress": progress, "unique_id":unique_id]
          NSNotificationCenter.defaultCenter().postNotificationName("refreshProgressBar", object:myDict);
        }
    }

现在这里是表格视图控制器的部分

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)

    let row = indexPath.row
    let uploadRecordData = uploadfilesRecords[row]
    //edited answer  
    cell.progressView.uniqud_id = uploadRecord.unique_id
    cell.progressView.progress = uploadRecord.progress
    return cell
}

这是在更新刷新上传文件进度时刷新单元格的方法。

像这样的进度视图的子类

struct ProgressView:YourProgressView{
            var unique_id:int

            //Now add notification observer to your progress view
            NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressView), name: "refreshProgressBar", object: nil)


            func refreshProgressView(notification: NSNotification){
                let dict = notification.object as! NSDictionary
                let progress = dict["progress"]
                let u_id = dict["unique_id"]

                if u_id == self.unique_id {
                    self.progress = progress
                }
            }

请参阅上面更新的操作子类和表视图委托方法中的代码。

【讨论】:

  • 谢谢@SachinVsSachin。我已经完成了 struct item { var title : String!变量大小:字符串! //var downloadStatus : DownloadStatus = .none init(title: String, size: String) { self.title = title self.size = size } } 子类是什么??
  • @ApplePrime 跟随此链接到操作的子类。gist.github.com/Sorix/57bc3295dc001434fe08acbb053ed2bc
  • 我今天正好遇到了问题,对我来说,文件正在很好地上传异步,但 tableview 单元格数据没有为特定的索引值重新加载。现在,如果我上传文件 tableview 单元格插入和加载良好但如果我上传下一个文件第二个单元格开始上传但前一个单元格冻结但文件上传。如何解决这个问题?
  • @ApplePrime 我已经更新了我的答案,请检查一下。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多