问题
I found this very interesting approach (Parse : is it possible to follow progress of PFObject upload) and I tried to convert the objective-c category in a swift extension. But I am overstrained with the values types unsigned long
.
Please have a look in the following code - it throws the exception (line: let progress:Int32 = Int32(100*count/numberOfCyclesRequired)
): fatal error: floating point value can not be converted to Int32 because it is either infinite or NaN.
I am also not sure how to handle the __block
prefix in swift that the count changes will also occur out of 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.
来源:https://stackoverflow.com/questions/29702451/ios-multiple-save-of-parse-objects-with-progress-bar