iOS Multiple save of parse objects with progress bar

♀尐吖头ヾ 提交于 2020-01-24 06:38:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!