I have an operation queue setup as follows:
let queue = OperationQueue()
queue.name = "com.company.myQueue"
queue.qualityOfService = .userInitiated
queue.maxConcurrentOperationCount = 64
...
var current = 0
var totalCount = someArray.count
for i in someArray {
...
let op = BlockOperation {
... // do some database queries
current += 1
}
op.completionBlock = {
DispatchQueue.main.async {
let nData: [String: CGFloat] = ["value": CGFloat(current/totalCount)]
NotificationCenter.default.post(name:Notification.Name(rawValue: "update"), object: nil, userInfo: nData)
}
queue.addOperation(op)
}
In my ViewController
I listen for the notification and update a UILabel
with the percentage. Problem is though I don't get any intermediate values... it jumps from being 0
directly to 100
once all the operations are done.
What am I doing wrong?
Thanks
The problem is CGFloat(current / totalCount)
. That does the integer math first, and then converts it to a float. You should flip that around to CGFloat(current) / CGFloat(totalCount)
.