I found this very interesting approach ( Parse: is it possible to track the progress of the PFObject download ) and I tried to convert the objective-c category to a quick extension, But I am haunted by type values unsigned long.
Please review the following code - it throws an exception (line:) let progress:Int32 = Int32(100*count/numberOfCyclesRequired): fatal error: floating-point value could not be converted to Int32, because it is either infinite or NaN. I'm also not sure how to handle the prefix __blockin swift so that counter changes also come from the block.
extension PFObject {
class func saveAllInBackground(objects: [AnyObject]!, chunkSize:Int, block: PFBooleanResultBlock!, progressBlock:PFProgressBlock) {
let numberOfCyclesRequired:Double = Double(objects.count / chunkSize)
var count:Double = 0
PFObject.saveAllInBackground(objects, chunkSize: chunkSize, block: block) { (trig:Bool) -> Void in
count++
let progress:Int32 = Int32(100*count/numberOfCyclesRequired)
progressBlock(progress)
}
}
class func saveAllInBackground(objects: [AnyObject]!, chunkSize:Int, block: PFBooleanResultBlock!, trigger:(Bool) -> Void) {
let range = NSMakeRange(0, objects.count <= chunkSize ? objects.count:chunkSize)
var saveArray:NSArray = (objects as NSArray).subarrayWithRange(range)
var nextArray:NSArray = []
if range.length < objects.count {
nextArray = (objects as NSArray).subarrayWithRange(NSMakeRange(range.length, objects.count-range.length))
}
PFObject.saveAllInBackground(saveArray) { (succeeded:Bool, error: NSError!) -> Void in
if (error == nil && succeeded && nextArray.count != 0) {
trigger(true)
PFObject.saveAllInBackground(nextArray, chunkSize: chunkSize, block: block, trigger: trigger)
} else {
trigger(true)
block(succeeded,error)
}
}
}
}
Thanks for your help in advance.