1

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.

PiyushRathi
  • 956
  • 7
  • 21
  • As i am understanding, do you want to upload multiple files to s3 and show the progress for them ? – Samarth Kejriwal Aug 14 '18 at 10:48
  • Yes.@ Samarth Kejriwal....I want to upload multiple files to s3 and show the progress in tableview cell. I have done upload process but I am stuck in tableview cell display. File A uploading time if i upload file B then file A is stopped. I think I have done mistakes in tableview data reload or something –  Aug 14 '18 at 10:51
  • means you want to upload multiple files simultaneously on s3 and show the upload progress for the corresponding cells? – Samarth Kejriwal Aug 14 '18 at 10:52
  • See what you can do is upload one file on server and then upload the next one on failure or success callback of s3 server response, if it is a success then remove the uploaded-file from your array and upload next one , if it is a failure do not remove that file from array and proceed to upload next file. Do this until your array becomes empty. – Samarth Kejriwal Aug 14 '18 at 10:55
  • for parallel uploading u will need multiple instance of transfer utility, it appears currently you have one instance of transfer utility that is what pauses the previous upload and starts a new one – Shahzaib Qureshi Aug 14 '18 at 10:57
  • for paralle upload you will have to create multiple instances of your transfer utility , its better you follow what i told – Samarth Kejriwal Aug 14 '18 at 11:02
  • upload one file on server and then upload the next one on failure or success callback of s3 server response, if it is a success then remove the uploaded-file from your array and upload next one , if it is a failure do not remove that file from array and proceed to upload next file - If we do in this way users need to wait one file upload completion right? I am excepting parallel uploads.@ Samarth Kejriwal –  Aug 14 '18 at 11:21
  • @Shahzaib Qureshi - Yes you got my problem. If i upload next one then first one paused. But both should work...Could you please provide some sample code part –  Aug 14 '18 at 11:24
  • i would need the code where you create the instance of transferUtility – Shahzaib Qureshi Aug 14 '18 at 11:52
  • @ShahzaibQureshi see this is TransferManager https://github.com/awslabs/aws-sdk-ios-samples/blob/master/S3TransferManager-Sample/Swift/S3TransferManagerSampleSwift/UploadViewController.swift But I am doing TransferUtility –  Aug 14 '18 at 12:17

1 Answers1

3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
SachinVsSachin
  • 6,401
  • 3
  • 33
  • 39
  • Thank you @SachinVsSachin. I have done struct item { var title : String! var size : String! //var downloadStatus : DownloadStatus = .none init(title: String, size: String) { self.title = title self.size = size } } what is the subclass?? –  Aug 14 '18 at 13:38
  • @ApplePrime follow this link to subclass of operation.. https://gist.github.com/Sorix/57bc3295dc001434fe08acbb053ed2bc – SachinVsSachin Aug 15 '18 at 01:48
  • I got my issues exactly today, for me file is uploading Async well but tableview cell data not reloading for particular index value. now, if i upload file tableview cell inserting and loading well but if i upload next file second cell started to upload but previous one got freezes but file uploaded.How to solve this? –  Aug 15 '18 at 04:47
  • @ApplePrime I have updated my answer Please check it. Thanks – SachinVsSachin Aug 16 '18 at 05:58