Brolivar
Brolivar

Reputation: 49

ios - Firebase Storage URL retrieval or retrieve from Firebase Database

  1. Retrieve the image URL using the Firebase Storage method storageReference.downloadURL() to retrieve the url:

Example:

    storageDownloadReference(completion: { url in 
    let imageURL = url
    }
  1. After you upload the image to Storage, and get the downloadURL, save the URL in Firebase Database, so you can retrieve it later.

Example:

// Save the URL, after storing the image.

    ref.child("items").child(item).setValue(["imageURL": downloadURL])

// And then read the URL from the database:

    ref.child("items").child(item).observeSingleEvent(of: .value, with: { (snapshot) in
      let value = snapshot.value as? NSDictionary
      let imageURL = value?["imageURL"] as? URL ?? ""

Once you have uploaded an image to Firebase Storage, which way of retrieving it is recommended?

Which option is recommended/more efficient?

Upvotes: 0

Views: 64

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317657

Neither one is superior. I think you should do whatever best meets the needs of your app.

If you need the client to download directly from a URL with universal access to anyone with that URL, then do that.

If you need the client app to authenticate itself in order to check if the user should be able to download the file using security rules, then use the SDK to download the file.

Download URLs are not affected by security rules, so if you need to use rules, don't use a download URL. If you need the file to be accessible by anyone on the internet who simply has that URL, then use a download URL.

If it doesn't matter based on the criteria I've illustrated here, then do whatever you find most convenient.

Upvotes: 1

Related Questions