skip to Main Content

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?

2

Answers


  1. 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
    }
    
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search