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)
}
}
}