首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >迅速将多个文件并行上载到AWS S3中,并在tableview单元格中显示进度视图状态

迅速将多个文件并行上载到AWS S3中,并在tableview单元格中显示进度视图状态
EN

Stack Overflow用户
提问于 2018-08-14 10:28:42
回答 1查看 2.3K关注 0票数 1

我是新来的AWS,我做了一些文件上传到AWS S3与TransferUtility文件转换。在这里,我的场景步骤

1.从iCloud中选择文件

代码语言:javascript
复制
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.使用传输实用程序将该文件上载到AWS S3中

代码语言:javascript
复制
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.当上传需要显示每个文件上载状态到表格视图单元格

代码语言:javascript
复制
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

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

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

EN

回答 1

Stack Overflow用户

发布于 2018-08-14 12:57:21

要做到这一点,您需要创建一个operations,并且每个上传文件都在操作中写入网络请求,并将这些操作添加到队列中。

我在这里暗示要这样做。

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

代码语言:javascript
复制
struct UploadRecordData { 
    let fileName:String
    let unique_id:String
    let progress:double
    //...etc
}

然后像这样的子类操作

代码语言:javascript
复制
    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);
        }
    }

下面是表视图控制器的一部分

代码语言:javascript
复制
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
}

以下是更新刷新上载文件进度时刷新单元格的方法。

进度视图的子类,如下面的

代码语言:javascript
复制
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
                }
            }

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51839307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档