2

I used this tutorial to add data to Firebase. It works perfectly. It also works offline thanks to the snapshots and persistence.

I also added images to Firebase storage from the this link. This also works, but not for persistence. I have tried to replicate the snapshot from the data (first tutorial) to do this, but FIRStorageReference does not have snapshot options as far as I can see. How can I do this?

import Foundation
import Firebase
import FirebaseStorage

struct MenuItemImage {

let image: NSData
let ref: FIRStorageReference?
let key: String



init(image: NSData, key: String = "") {
    self.key = key
    self.image = image
    self.ref = nil

}

init(snapshot: FIRDataSnapshot) { // this should be something like FIRStoreSnapshot
    key = snapshot.key
    let snapshotValue = snapshot.value as! [String: AnyObject]
    image = snapshotValue["image"] as! NSData
    ref = snapshot.ref // errors as cannot assign value of tupe FIRDatabaseReference to type FIRStorageReference
}

func toAnyObject() -> Any {
    return [
        "image": image
    ]
}

}
Community
  • 1
  • 1
ThundercatChris
  • 481
  • 6
  • 25
  • Can you elaborate more on what you mean by "image persistence" ? – Bruno Recillas Apr 04 '17 at 22:23
  • Sure, For normal data (strings), the code I am using creates an array that will still hold the Strings even when the device is offline. Firebase acts like NSUserDefaults or Core data. Without any real setup. However Images are stored in a different area of Firebase so when the device is offline, the strings still appear but the images are gone (arrays empty) – ThundercatChris Apr 04 '17 at 22:34
  • 1
    Firebase Storage and Firebase Database are two different products. The Firebase Database SDK can handle disk persistence for you. The Firebase Storage SDK does not. To cache images that you get from Firebase Storage, you'd typically use a third party library. See any of these: http://stackoverflow.com/search?q=%5Bfirebase-storage%5D%5Bios%5D+cache+images – Frank van Puffelen Apr 04 '17 at 23:34
  • Yes you need to cache an image. You could create your own cache extension. Refer to this video: https://youtu.be/GX4mcOOUrWQ – Bruno Recillas Apr 06 '17 at 02:36

2 Answers2

2

Using FirebaseUI/Storage: add to your Podfile:

pod 'FirebaseUI/Storage'

Then, in your code, to show and cache an image do:

import FirebaseStorageUI

let imgRef = Storage.storage().reference().child("images/\(blah)")
myImageView.sd_setImage(with: imgRef))

That's it! The image will be fetched in the background, and cached for next time, if the same reference is used.

jazzgil
  • 2,250
  • 2
  • 19
  • 20
  • Great solution. This way we do not need to first download the firebase storage url and then use this url with an image caching library. – Crt Gregoric Apr 19 '19 at 14:03
0

Kingfisher worked really well thanks, however I do have a few issues with it being slow now (see separate thread)

Kingfisher with Firebase storage slow to display images - swift 3

Oh and I fixed the snapshot by including a blank imageURL in the struct, then writing to it when the image was saved into FirebaseStorage. it meant the struct came back in the snapshot with the URL

Community
  • 1
  • 1
ThundercatChris
  • 481
  • 6
  • 25