I'm trying to upload Several separate images, as long as I upload them 1 at the time it works. But once I try to upload the next image or more than one image the problems starts. every image I upload in a series will update all images with the progression of the latest image uploaded. When I took more than one images before the upload of the last one was done it will disturb the progression of the others by showing various upload progression numbers randomly.
this is how I update my UI:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "uploadQueueCell", for: indexPath) as! UploadQueueCustomCell
if cell.progress.text?.lowercased() == "percentage".lowercased()
{
cell.progress.text = "0"
}
if indexPath.row < uploader.GetUploadQueue().Count()
{
let item = uploader.GetUploadQueue().Get(pos: indexPath.row)
cell.filename.text = item._FileName
let percentage = String(item._Percentage)
cell.progress.text = percentage
cell.cell_image.image = UIImage(data: item._ImageData)
}
updateView()
return cell
}
public func updateView() {
DispatchQueue.main.async
{
self.tableView.reloadData()
}
}
this is how the individual Items in my array is stored :
public class UploadQueueCellData
{
public let _FileName:String
public let _UploadTaskDelegate:URLSessionTaskDelegate
public let _ImageData:Data
public let _TaskIdentifier:Int
public init(fileName:String,imageData:Data,uploadTaskDelegate:URLSessionTaskDelegate,taskIdentifier: Int)
{
_FileName = fileName
_ImageData = imageData
_UploadTaskDelegate = uploadTaskDelegate
_TaskIdentifier = taskIdentifier
}
public var _Percentage:Int
{
get
{
let test = _UploadTaskDelegate as! UploadDelegate
let returnval = test._percentage
return returnval
}
}
}
and my delegate for uploading progression
public class UploadDelegate: URLSessionUploadTask, URLSessionTaskDelegate {
public var _response:HTTPURLResponse = HTTPURLResponse()
public var _error:String? = nil
public var _percentage:Int = 0
public var _Filename:String? = nil
public var _urlSessionTask:URLSessionTask? = nil
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)
{
_error = error.debugDescription
}
public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)//->Int
{
//Progression
let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend)
_urlSessionTask = task
DispatchQueue.main.async
{
self._percentage = Int(progress * 100)
}
}
}
I'm at a loss how to separate the individual uploads and keep track of their progression everything I do seem to either work towards only being able to track the last upload. or update all of them with the random progression numbers instead of the progression belonging to the individual uploads. is there a way to actually make my model separate the individual uploads and keep track of them separately ? and how would I go about doing that ?
Edit:
I think i should add that I send one image at a time but because of connectivity or slow speed could end up with a cached queue of sent items that still is not complete. I'm simply trying to show progression of individual items already send.
I found a solution.
My solution was to try and keep track of which file I was receiving the progression on. I know it's probably not the neat solution but it's working atm I did it in this way:
public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)//->Int
{
//Progression
_Filename = task.currentRequest?.value(forHTTPHeaderField: "filename")
let progress = Float(totalBytesSent)/Float(totalBytesExpectedToSend)
_urlSessionTask = task
DispatchQueue.main.async
{
self.objQueue?.Get(filename: self._Filename!)._Percentage = Int(progress * 100)
// self._percentage = Int(progress * 100)
}
}
the rest is pretty much the same as before.
with one small exception:
public class UploadQueueCellData//:NSObject, UploadURLSessionTaskDelegate
{
public let _FileName:String
public let _UploadTaskDelegate:URLSessionTaskDelegate
public let _ImageData:Data
public let _TaskIdentifier:Int
public init(fileName:String,imageData:Data,uploadTaskDelegate:URLSessionTaskDelegate,taskIdentifier: Int)
{
_FileName = fileName
_ImageData = imageData
_UploadTaskDelegate = uploadTaskDelegate
_TaskIdentifier = taskIdentifier
}
private var intPercentage:Int = -1
public var _Percentage:Int
{
get
{
return self.intPercentage
}
set
{
self.intPercentage = newValue
}
}
}