我正在尝试将文件上传到AWS S3中。我从iCloud中选择文件,并附加到array中以在tableView中列出,同时调用AWS S3 upload函数。

在这里,一旦文件成功上传,我就需要在array中进行标识,因为每当我尝试上传下一个file时,我的数组就会再次推送已经是stored的文件。因此,上传过程给出了duplication结果。如何避免这个问题?

我的麻烦是,如果我从数组中删除了上传的文件url,那么我将无法在tableView中列出,但是如果我没有删除,那么当我尝试上传新文件时,它将是re-uploading

// Array declaration
var items = [Item]()
var fileArray = [Item]()

// Values appending into my array
items.append(Item(indexPath: indexPath, file: item.url))

// TableView data-load
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
    let item = fileArray[indexPath.row]

       if fileArray.count > 0 {
            cell.url_label.text = item.url

            // Upload function call
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
                self.uploadData(indexPath: indexPath, file: item.url)
            })
       }
   return cell
}

// Upload function with parameters
private func uploadData(indexPath: indexPath, file: item.url) {

   // Tracking each cell and it's file.
   // Once, file uploaded I am doing some changes in tableview cell
}

最佳答案

1-您不应在其中执行任何非UI任务

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {

2-您不应该在cellForRowAt中上载文件,因为每次滚动时它都会被调用,因此请确保您会遇到重复,如果您只希望对可见单元格执行此操作,则在数组模型中创建Enum类型的status属性以指示该文件的当前状态-> NotUploaded-Uploaded-Uploading并在上传前进行检查
Enum Status {
  case  notUploaded,uploaded,uploading
}

class Item {
  var st:Status = .notUploaded
  func upload () {
    if st == .notUploaded {
      st =.uploading
      // upload here
      API.upload {
        self.st = .uploaded
      }
    }
  }
}

关于ios - 如何使用Swift 4避免文件上传重复,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52679404/

10-08 22:50
查看更多