0

Correct me if I'm wrong, but when I try to update my label in a while loop it won't update it. Is the reason that the while loop runs too quickly for the label to update within that time so it'll cancel the label from updating?

I need a solution to this? I need to be able to update the label straight away?

func Download(complete: (canMoveOn: Bool) -> Void) {
    isDownload = true

    if (connected!) {
        Reset()

        let data = NSData(contentsOfFile: path!)!
        let buffer = (UnsafeMutablePointer<UInt8>(data.bytes))

        var leftOverSize = data.length
        let bytesFile = data.length
        var totalBytesRead = 0
        var bytesRead = 0

        let stream = CFReadStreamCreateWithFTPURL(nil, downloadURL!).takeUnretainedValue()
        let status = CFReadStreamOpen(stream)

        if (status == true) {
            while (totalBytesRead < bytesFile) {
                bytesRead = CFReadStreamRead(stream, buffer, leftOverSize)

                if (bytesRead > 0) {
                    totalBytesRead += bytesRead

                    if (bytesRead < bytesFile) {
                        leftOverSize = bytesFile - totalBytesRead
                    } else {
                        leftOverSize = 0
                    }
                } else {
                    break
                }

                //-------------Not Updating Label
                downloadLabel.attributedText = NSAttributedString(string: "\(totalBytesSent)", attributes: [NSFontAttributeName: UIFont(name: "Arial", size: 20)!, NSForegroundColorAttributeName: UIColor.darkTextColor()])

                Calculate(bytesRead, totalBytesSent: totalBytesRead, totalBytesExpectedToSend: bytesFile)
            }

            CFReadStreamClose(stream)

            complete(canMoveOn: true)
        } else {
            Error()

            downloadLabel.attributedText = NSAttributedString(string: "- -", attributes: [NSFontAttributeName: UIFont(name: "Arial", size: 20)!, NSForegroundColorAttributeName: UIColor.darkTextColor()])

            complete(canMoveOn: false)
        }
    }
}
HovyTech
  • 299
  • 5
  • 19
  • how do you update currentSize? – Encio Peter Feb 01 '16 at 09:03
  • It could be also a thread issue. The UI is supposed to be updated on the main thread. – vadian Feb 01 '16 at 09:04
  • What's the main thread? Is that the initial UIViewController? – HovyTech Feb 01 '16 at 09:12
  • Thread is the "environment" of the run loop the code runs in. It's the train while your controller is the passenger. ;-) – vadian Feb 01 '16 at 09:15
  • So I have a UIViewController Class, InternetSpeed, and another UIViewController Class, SpeedController. The SpeedController is connected to my storyboard but is set up like `Class SpeedController: InternetSpeed {...}` – HovyTech Feb 01 '16 at 09:20

1 Answers1

2

Try Below Code, this will surely works.

 while (currentSize < fileSize) {

        dispatch_sync(dispatch_get_main_queue(), { () -> Void in
            downloadLabel.attributedText = NSAttributedString(string: "\(currentSize) kbps", attributes: [NSFontAttributeName: UIFont(name: "Arial", size: 20)!, NSForegroundColorAttributeName: UIColor.darkTextColor()])
            });

    }

Just paste your UI update code to main thread as shown above. Reason : You can't update your UI from background thread.

Hitendra Solanki
  • 4,871
  • 2
  • 22
  • 29