Home > database >  Progress bar in tableview cell?
Progress bar in tableview cell?

Time:09-30

How do I use a progress bar when my script is doing some task that is likely to take time?

For example, a function which takes some time to complete and returns True when done. How can I display a progress bar during the time the function is being executed?

Note that I need this to be in real time, so I can't figure out what to do about it. Do I need a thread for this? I have no idea.

Right now I am not printing anything while the function is being executed, however a progress bar would be nice. Also I am more interested in how this can be done from a code point of view.

func callApi() {
    let group = DispatchGroup()

    for (indexVal, ref) in arrayURL.enumerated() {
        group.enter()
        let progressQueue = DispatchQueue(label: "com.alamofire.progressQueue", qos: .utility)

        AF.download(ref)
            .downloadProgress(queue: progressQueue) { progress in
                print("Download Progress: \(progress.fractionCompleted)")
                
                DispatchQueue.main.async {
                    guard let cell = self.tableProgress.cellForRow(at: NSIndexPath.init(row: indexVal, section: 0) as IndexPath) as? ProgressCell else {
                        return // or fatalError() or whatever
                    }
                    cell.progressView.progress = Float(progress.fractionCompleted)
                }
            }
            .responseData { response in
                if let data = response.value {
                    print("===============>", indexVal)
                    group.leave()
                    print(data)
                }
            }
    }
    group.notify(queue: .main, execute: { // executed after all async calls in for loop finish
        print("done with all async calls")
    })
}

CodePudding user response:

We can create a progressView like this.Note that I need this to be in real time, so I can't figure out what to do about it. Do I need a thread for this? I have no idea.

let progressView: UIProgressView = {
    let view = UIProgressView()
    view.progressTintColor = UIColor.blue
    view.progress = 0
    return view
  }()

CodePudding user response:

You can use below code, its working for me.

        let group = DispatchGroup()
        
        let folder = try! FileManager.default
            .url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            .appendingPathComponent("SourceFiles")

        for (indexVal, ref) in arrayURL.enumerated() {
            
            let destination: DownloadRequest.Destination = { _, _ in
                    let fileURL = folder.appendingPathComponent(ref.lastPathComponent)
                    return (fileURL, [.createIntermediateDirectories])
                }
            group.enter()
                        let progressQueue = DispatchQueue(label: "com.alamofire.progressQueue", qos: .utility)

            AF.download(ref, to: destination).downloadProgress(queue: progressQueue) { progress in
                                   print("Download Progress: \(progress.fractionCompleted)")
                
                DispatchQueue.main.async {
                    guard let cell = self.tableview.cellForRow(at: NSIndexPath.init(row: indexVal, section: 0) as IndexPath) as? UITableviewCell else {
                        return 
                    }
                    cell.progressView.progress = Float(progress.fractionCompleted)
                }
            }.response { responseData in
                   switch responseData.result {
                   case .success(let url):
                       guard let url = url else { return }
                       do {
                           let unzipDirectory = try Zip.quickUnzipFile(url)

                       }
                       catch{
                           
                       }
                       group.leave()

                   case .failure(let error):
                       print(error)
                   }
               }

        }
        group.notify(queue: .main, execute: { // executed after all async calls in for loop finish
 
        })
  • Related