2

I am using the Giphy API and FLAnimatedImage respository to fetch and show gifs. Is there a way to store selected gifs to firebase or firestore that I can then fetch later?

My reason is to avoid the intensive task of re-downloading the gif. Would it be storing the bytes of the Data object?

I understand I could probably just store the gif url and then when I need the gif, to query the string and re-download, but I'm hoping to again avoid that if there's a way.

Any guidance would be appreciated.

let url = URL(string: gifUrlString)!
let imageData = try? Data(contentsOf: url)
let image = FLAnimatedImage(animatedGIFData: imageData)
Chris
  • 431
  • 8
  • 33
  • I had answered a [similar question](https://stackoverflow.com/questions/33644560/swift2-retrieving-images-from-firebase/34044446#34044446) back in 2015 - that code shows how to store and retrieve a .jpg to/from Firebase. The thing that is confusing as you want to *avoid the intensive task of re-downloading the gif* - if you are storing it in Firebase you will have to re-download it. Additionally, Firebase Realtime Database is *not* the best solution for storing images - Firebase has [Firebase Storage](https://firebase.google.com/docs/storage/ios/start) which is perfect for that task. – Jay May 16 '19 at 20:01
  • @Jay thanks for the message. if you'll allow me as im not super handy in this, i had thought perhaps if we already downloaded the gif and had it in bytes, and potentially storing that, all that's left in terms of a future task is to fetch the bytes from Firebase and then render it into the gif versus calling and re-downloading from the api. let me know if that changes anything or not, thanks! – Chris May 17 '19 at 01:35
  • If you *fetch the bytes from Firebase*, you are downloading the bytes from Firebase which is exactly the same thing as downloading the gif from Firebase. Bytes are bytes either way. Also. *rendering to a gif* is just a set of bytes and there is no rendering involved. Not sure what *re-downloading from the API* means as 'downloading' is what Firebase does. It's an online real-time database and any interactions with it are downloaded/uploaded. The exception is if you have a temporary disconnection which is where persistence comes in. Maybe you are thinking that Firebase is a local database? – Jay May 17 '19 at 14:17

1 Answers1

0

You can store the data of the image

let imageData = try? Data(contentsOf: url)

with Firebase putData function

but you still need to download it again with firebase getData , so you may create a simple database structure in your app with CoreData / Realm associate each gif say with timestamp of it's download , then upload it to Firebase and update your local database with these data

BTW avoid using Data(contentsOf as it blocks the main thread until the date grabbed so use it inside a background thread

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87