Iam Wayne
Iam Wayne

Reputation: 561

Firebase download progress observer error Swift 3

Trying to show firebase download progress, getting error

"value of type FIRStorageRef has no member observe".

This is the code I got from firebase documents and trying to use.

    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)
            }


}

Any help would be appreciated, thanks in advance.

Upvotes: 1

Views: 1660

Answers (1)

nathan
nathan

Reputation: 9395

downloadURL() doesn't generate a download task like the other download functions, as per Download files - Monitor download progress, since it only returns the download URL and not the file's data.

You'll need to use one of the following: write(toFile:) or getData(maxSize:) and finally task.observeStatus(.progress). The first downloads it to a local file while the latter does so in memory.

Sample from the docs (full source):

// 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")
}

Upvotes: 5

Related Questions