如何将Firebase下载文件与进度视图相关联?

以下是我用于从Firebase存储下载PDF文件的代码。

我想要做的是将其与包含百分比而非活动指示符的下载进度视图链接。

import Foundation import UIKit import Firebase import SwiftLoader class showPdfVC: UIViewController , UIWebViewDelegate,ReaderViewControllerDelegate{ var pdfbooks = UIWebView() var nIndex:NSInteger! var post: Post! var db : DBHelper = DBHelper() var book : BookModel? @IBAction func backbtn(_ sender: Any) { if let navController = self.navigationController { navController.popViewController(animated: true) } } override func viewDidLoad() { super.viewDidLoad() var config : SwiftLoader.Config = SwiftLoader.Config() config.size = 150 config.spinnerColor = .brown config.foregroundColor = .black config.foregroundAlpha = 0.5 config.titleTextColor = .brown SwiftLoader.setConfig(config) if "" != book?.bookPath { // self.activityIND.isHidden = true // self.activityIND.stopAnimating() UIApplication.shared.isNetworkActivityIndicatorVisible = false SwiftLoader.hide() loadReader(filePaht: (book?.bookPath)!) } else { let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let strName = book?.id let filePath = "\(documentsPath)/"+strName!+".pdf" let fileManager = FileManager.default // self.activityIND.startAnimating() SwiftLoader.show(title: "Loading...", animated: true) UIApplication.shared.isNetworkActivityIndicatorVisible = true if fileManager.fileExists(atPath: filePath) { // self.loadFromUrl(path: filePath) loadReader(filePaht: (book?.bookPath)!) return; } let reference = FIRStorage.storage().reference(forURL: (self.book?.bookURL)!) reference.data(withMaxSize: 50 * 1024 * 1024) { (data, error) -> Void in if (error != nil) { print ("unable to download pdf file from Firebase Storage") // self.activityIND.isHidden = false // self.activityIND.startAnimating() SwiftLoader.show(title: "Loading...", animated: true) UIApplication.shared.isNetworkActivityIndicatorVisible = true } else { if ((try! data?.write(to: URL.init(fileURLWithPath: filePath, isDirectory: false))) != nil) { // self.loadFromUrl(path: filePath) print ("pdf file is downloaded from Firebase Storage") self.db.upDate(id: (self.book?.id)!, bookPath: filePath) // self.activityIND.isHidden = true SwiftLoader.hide() self.loadReader(filePaht: filePath) UIApplication.shared.isNetworkActivityIndicatorVisible = false } } } } } func loadReader(filePaht : String) { let document = ReaderDocument(filePath: filePaht, password: nil) if document != nil { let readerVC = ReaderViewController(readerDocument: document) readerVC?.delegate = self readerVC?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve readerVC?.modalPresentationStyle = UIModalPresentationStyle.fullScreen self.navigationController?.pushViewController(readerVC!, animated: true) } } func dismiss(_ viewController: ReaderViewController!) { _ = self.navigationController?.popToRootViewController(animated: true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 

几点想法:

  • 不要在内存中下载然后写入磁盘 – 我们有一个直接下载到文件系统的方法
  • 附加侦听器而不是单个完成处理程序 – 这样您就可以监听进度更新
  • 使用类似MBProgressHUD的内容来显示增量进度条
  • 不需要设置isNetworkActivityIndicatorVisible因为我们会为您执行此操作

把它们放在一起,就会得到类似的东西:

 // Create a reference to the file we want to download let starsRef = storageRef.child("images/stars.jpg") // Start the download (in this case writing to a file) let downloadTask = storageRef.write(toFile: localURL) // Start progress indicator // Observe changes in status downloadTask.observe(.progress) { snapshot in // Download reported progress let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount) / Double(snapshot.progress!.totalUnitCount) // Update the progress indicator } downloadTask.observe(.success) { snapshot in // Download completed successfully // Stop progress indicator } // Errors only occur in the "Failure" case downloadTask.observe(.failure) { snapshot in // An error occurred! // Stop progress indicator }