4

我发现了这种非常有趣的方法(解析:是否可以跟踪 PFObject 上传的进度)并且我尝试在快速扩展中转换目标 c 类别。但我对值类型过度紧张unsigned long
请查看以下代码 - 它抛出异常(行:)let progress:Int32 = Int32(100*count/numberOfCyclesRequired)致命错误:浮点值无法转换为 Int32,因为它是无限的或 NaN。 我也不确定如何快速处理__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)
        }
    }
}

}

提前感谢您的帮助。

4

0 回答 0