Firebase下载进度观察者错误Swift 3

时间:2017-08-22 03:36:54

标签: ios firebase swift3 firebase-realtime-database firebase-storage

尝试显示firebase下载进度,收到错误

  

“FIRStorageRef类型的值没有成员观察”。

这是我从firebase文档中获取并尝试使用的代码。

    storage = FIRStorage.storage()

    let storageRef = storage.reference().child("Audio").child(successFileName)
    self.titleLabel.text =  self.successTitlename

    SwiftSpinner.show("Loading...")

    storageRef.downloadURL { url, error in
        if error != nil {

            SwiftSpinner.show("Couldn't load Audio...Tap to dismiss").addTapHandler({
                SwiftSpinner.hide()
            })
       **Getting error here** 
            storageRef.observe(.progress) { snapshot in
 // Download reported progress
    let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
                    / Double(snapshot.progress!.totalUnitCount)
            }


}

任何帮助将不胜感激,提前谢谢。

1 个答案:

答案 0 :(得分:3)

根据{{​​3}},

downloadURL()不会像其他下载功能一样生成下载任务,因为它只返回下载网址而不是文件的数据。

您需要使用以下某项之一:write(toFile:)getData(maxSize:),最后使用task.observeStatus(.progress)。第一个将它下载到本地文件,而后者在内存中这样做。

来自文档(Download files - Monitor download progress)的示例:

// Create a reference to the file we want to download
let starsRef = storageRef.child("images/stars.jpg")

// Create local filesystem URL
let localURL = URL(string: "path/to/stars.jpg")!

// Start the download (in this case writing to a file)
let downloadTask = storageRef.write(toFile: localURL)

// Download in memory with a maximum allowed size of 10MB
// let downloadTask = storageRef.getData(maxSize: 10 * 1024 * 1024)

downloadTask.observe(.progress) { snapshot in
  // Download reported progress
  let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
    / Double(snapshot.progress!.totalUnitCount)
  print("Done: \(percentComplete)%")
}

downloadTask.observe(.success) { snapshot in
  // Download completed successfully
  print("Downloaded successfully")
}