使用 Firebase 链接 UIProgressView 以显示下载进度

linking UIProgressView to show download progress using Firebase

我试图显示我正在下载的文件的进度视图,当我打印进度时它是正确的但是当我在进度视图上实现它时它完全错误它会达到 3000% 这是我的代码。我正在使用 MHRadialProgressView

downloadTask.observe(.progress) { (snapshot) -> Void in
    // Download reported progress

    let percentComplete = 100 * Double(snapshot.progress!.completedUnitCount) / Double(snapshot.progress!.totalUnitCount)
    print("percentComplete")
    self.progressView.moveNext(currentprogress as NSNumber!)

    // Update the progress indicator
}

当我使用 MBProgressHUD 时,这里是它的代码

  downloadTask.observe(.progress) { (snapshot) -> Void in
                // Download reported progress    
                let percentComplete = 100 * Float(snapshot.progress!.completedUnitCount)
                    / Float(snapshot.progress!.totalUnitCount)

            let hud = MBProgressHUD.showAdded(to: (self.navigationController?.view)!, animated: true)
            // Set the determinate mode to show task progress.
            hud.mode = MBProgressHUDMode.determinate

            hud.label.text = NSLocalizedString("Loading...", comment: "HUD loading title")
            // Set up NSProgress
            let progressObject = Progress(totalUnitCount: 100)
            hud.progressObject = progressObject
            // Configure a cancel button.


            DispatchQueue.global(qos: .default).async(execute: {() -> Void in
                // Do something useful in the background and update the HUD periodically.

                while progressObject.fractionCompleted < 1.0 {

                    progressObject.becomeCurrent(withPendingUnitCount: 1)
                    progressObject.resignCurrent()
                    usleep(useconds_t(percentComplete))
                }

                DispatchQueue.main.async(execute: {() -> Void in
                    hud.hide(animated: true)
                })
            })
        }

我收到这个错误 致命错误:无法将 Float 值转换为 UInt32,因为它是无限的或 NaN

几点建议:

  • 不要使用此库(自 2014 年以来未更新),请使用 MBProgressHUD, which allows you to set a progress object and forget about it
  • 如果您仍想使用原始库,请务必阅读文档——上面写着,"or unordered progres [sic] (often by steps associated wtih [sic] user action), you can increase the progress by value"。这意味着您需要将之前的进度与当前进度进行比较,然后自己递增或递减。