Swift将多个文件并行上传到AWS S3,并在tableview单元格中显示进度视图状态 [英] Swift upload multiple files parallel into AWS S3 and show progress view status in tableview cell
问题描述
我是AWS新手,我使用TransferUtility文件转换将一些文件上传到AWS S3。这里我的方案步骤
I am new for AWS, I have done some file uploading into AWS S3 with TransferUtility file transformation. Here my scenario steps
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]
}
我的问题:tableview我可以显示上传项目,但是当我上传下一个项目时首先上传停止。我需要实现并行上传多个文件并显示单元状态。
My Issue: The tableview I can able to show uploading items but first uploading stopped when I upload next one. I need to achieve parallel upload multiple files and show on cell status.
推荐答案
为此,您需要创建一个Operation队列,每个上传文件在操作内部写入网络请求并将这些操作添加到队列中。
To do that you create a Operation queue, and each upload file write network request inside of operation and add these operations to queue.
这里我给出提示来做这件事。
Here I am giving to hint to do this.
创建一个具有类似的属性的模型类p>
Create a model class that has properties like
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);
}
}
现在这里是表视图控制器的一部分
Now here is the part of table view controller
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
}
}
请参阅操作子类中的上述更新代码表视图委托方法。
Please see above updated code in Operation subclass and table view delegate method.
这是我的解决方案,请让我知道你明白了。谢谢快乐编码!!!
Here is my solution, please let me know you got it . Thanks Happy coding !!!
这篇关于Swift将多个文件并行上传到AWS S3,并在tableview单元格中显示进度视图状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!