问题
I am new for AWS, I have done some file uploading into AWS S3 with TransferUtility file transformation. Here my scenario steps
1. Picking the files from 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. Upload that file into AWS S3 with transfer-utility
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. While upload need to show each file uploading status into tableview cell
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellutil", for: indexPath) as! UtilityTableViewCell
let item = items[indexPath.row]
}
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.
回答1:
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.
Create a model class that has properties like
struct UploadRecordData {
let fileName:String
let unique_id:String
let progress:double
//...etc
}
and then sub-class of operation like this
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
}
Here is way to refresh cell while updating refresh upload files progress.
Sub-class of your progress view like this
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.
来源:https://stackoverflow.com/questions/51839307/swift-upload-multiple-files-parallel-into-aws-s3-and-show-progress-view-status-i