0

Ok, I have looked at Displaying download progress in reusable cells but am running into an issue using URL Session to track download progress across tableview cells, after reloading the table:

 func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        if totalBytesExpectedToWrite > 0 {
            let progress = Float(Double(totalBytesWritten) / Double(totalBytesExpectedToWrite))

            itemsDownloadingNow[(downloadTask.originalRequest?.url)!] = progress

            //update
            for (i, prog) in itemsDownloadingNow
            {
                if(i.lastPathComponent == episode?.enclosure?.url.lastPathComponent)
                {
                    //is downloading
                   circleProgress?.percentage = prog
                }
            }

        }
    }

This is what I've tried so far (this is in the TableViewCell class), storing the progress for each task by URL in a global dictionary: var itemsDownloadingNow = [URL: Float]()

This works if the table is NOT reloaded (the cells are shuffled), but AFTER reloading the circle progress goes back to zero/doesn't track. I don't know how to properly do this, and the only way to continuously update the circle is putting it in this URL session func.

How can I track the download progress per cell?

pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
blue
  • 7,175
  • 16
  • 81
  • 179
  • Save a Variable in dictionary with other details that is used to populate tableView , When table is reloaded progress bar value is to be that value saved when you update progress in cell save its progress status in a Dictionary or array as per your requirement and On reloading use that created Array or dictionary – iOS Geek Jun 09 '18 at 04:20
  • 1
    Here is your demo https://www.raywenderlich.com/158106/urlsession-tutorial-getting-started – Prashant Tukadiya Jun 09 '18 at 05:18
  • Simple solution would be to make a model class for the datasource of tableview and store the download progress each time progress is updated from api in relevant model. Now you need to just set that progress in *circleProgress?.percentage* in your case. – Dhaval H. Nena Jun 09 '18 at 08:18

0 Answers0