0

I want to store the progress of progressView into arraylist as one element

I decleared the array like this

var prog = [Float]()

and i'm getting the progress like this

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

    }
}

I tried this to store it

prog.insert(progressVi, at: 0)

but I'm getting it like this

[0.0071399305, 0.004518387, 0.002777841, 0.0012658517, 0.00086148235, 0.00063292583, 0.000580182, 0.0005274382, 0.0001557393]

and it's keep adding ...

So how can I store it as one element?

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Sam
  • 67
  • 1
  • 6

2 Answers2

1

You are going to the wrong direction. What you need is to get the dataTask progress object and use it to set your UIProgressView observedProgress property.

var observedProgress: Progress? { get set }

When this property is set, the progress view updates its progress value automatically using information it receives from the Progress object. (Progress updates are animated.) Set the property to nil when you want to update the progress manually. The default value of this property is nil. For more information about configuring a progress object to manage progress information, see Progress.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
0

Store single element

To store it as single element of the array simply:

var prog = [Float]()
prog = [progresVi]

If you insert the item at 0, the array will grow up every time you insert an element.

Store multiple elements

In order to store multiple progresses it is necessary to create a model to store the queue.

struct Element { 
    let id: UUID
    var progress: Float
}

var stack = [Element]()

It is necessary to store the ID for every item you are registering the progress. If you have the ID, then you can update the item progress:

let idForItem = UUID()

func getProgress() -> Float { ... }

let newProgress = getProgress()

// Update the stack of elements
if let elementIndice = stack.firstIndex(where: {$0.id == idForItem}) { 
     stack[elementIndice].progress = newProgress
}

AlbertUI
  • 1,409
  • 9
  • 26
  • well, it's work fine . but when I download another file . the second progress is add over the first ... but not as a second element – Sam Apr 28 '21 at 21:17
  • If you want to store multiple items you need to model that, let me code it as an edition... – AlbertUI Apr 28 '21 at 21:25