0

I am new to Swift3. I am uploading Video to Amazon S3 server from my Application and for each Video I have set one progress view. When I am adding a video that time my ProgressView is updating but my problem is when I am going back from the screen and come again then ProgressView is not updating. I checked using breakpoint then my Collection View cellForItemAt method is invoking, but progress view is not updating. I have added UIProgressView using Custom cell.

Here is my code:

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videocell", for: indexPath) as! VideoCell

        let videos = videoUrlsArr.object(at: indexPath.row) as! VideoModel

        if videos.uploadProgress == 1.0{
            cell.progressView.isHidden = true
        }
        else{
            cell.progressView.isHidden = false
            cell.progressView.setProgress(videos.uploadProgress, animated: true)
        }

        return cell
    }

//Upload Video file to S3

func uploadMP4MediatoAmazonS3(videos: VideoModel, data:Data) {

        let expression = AWSS3TransferUtilityUploadExpression()

        expression.progressBlock = { (task: AWSS3TransferUtilityTask, progress: Progress) in
            print(progress.fractionCompleted)
            DispatchQueue.main.async(execute: {

                self.progress = Float(progress.fractionCompleted)
                videos.uploadProgress = self.progress

                self.collectionView.reloadData()
                self.collectionView.collectionViewLayout.invalidateLayout()
                self.collectionView.layoutSubviews()

                print("Progress: \(self.progress)")
            })
        }//progress expression....

        self.uploadCompletionHandler = { (task, error) -> Void in
            DispatchQueue.main.async(execute: {
                if ((error) != nil){
                    print("Failed with error")
                    print("Error: %@",error!);
                }
                else if(self.progress != 1.0) {
                    print("Error: Failed - Likely due to invalid region / filename")
                }
                else{

                    videos.uploadProgress = 1
                    print("Success")
                }
            })
        }//completion handler for upload....


        let transferUtility = AWSS3TransferUtility.default()

        transferUtility.uploadData(data, bucket: S3BucketName, key: videos.videoPath, contentType: "", expression: expression, completionHander: uploadCompletionHandler).continue ({ (task:AWSTask) -> Any? in
            if let error = task.error {
                print("Error: %@",error.localizedDescription);
            }
            if let exception = task.exception {
                print("Exception: %@",exception.description);
            }
            if let _ = task.result {
                print("Upload Started...")
            }

            return nil;
        })

    }

//My custom cell class

import UIKit

class VideoCell: UICollectionViewCell {
    @IBOutlet weak var videoImage: UIImageView!

    @IBOutlet weak var playIcon: UIImageView!
    @IBOutlet weak var deleteBtn: UIButton!
    @IBOutlet weak var wbView: UIWebView!

    @IBOutlet weak var progressView: UIProgressView!
}

Please suggest me. Thank you!

user2786
  • 656
  • 4
  • 13
  • 35
  • Where is uploadMP4MediatoAmazonS3 defined? I mean in which class? Is it in a different NSObject Class for webservices? or in the same ViewController? – Hrishikesh Menon Jul 16 '18 at 10:26
  • It has defined in the same viewcontroller class like DispatchQueue.global(qos: .userInitiated).sync {self.uploadMP4MediatoAmazonS3(videos: videos, data: videos.filedata!)} – user2786 Jul 16 '18 at 10:26
  • Whats the value for videos.uploadProgress in cellForItem ? Is it the default value? If its the default value, then you may need to change the way you are calling the function – Hrishikesh Menon Jul 16 '18 at 10:32
  • Progress value is changing according to the progress. Even I have tried to hide other controls but that too is not working, but my cellForRow method code is executing. – user2786 Jul 16 '18 at 10:33
  • Even I tried using 'cell.progressView.setProgress(0.7, animated: true)', it's working when I am in the same screen but when I am going back and come again then progress view is not updating it's progress as 0.7 – user2786 Jul 16 '18 at 10:34
  • Check by reloading the collection view after some delay. – Hrishikesh Menon Jul 16 '18 at 10:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176049/discussion-between-user2786-and-hrishikesh-menon). – user2786 Jul 16 '18 at 10:49

0 Answers0