Elio_
Elio_

Reputation: 429

iOS/Swift: How to manage image downloading from Firebase in a feed

I'm trying to implement a feed for images using Firebase Storage, think of it like the Instagram photo feed. My Problem:

I tried downloading the images from the reference like this:

let storageRef = storage.reference()


    for i in 0...10 {

        let imageRef = storageRef.child("images/" + String(i) + ".jpg")

        imageRef.dataWithMaxSize((10 * 1024 *  1024), completion: { (data, error) -> Void in
            if (error != nil) {
                print(error)
            } else {
                let image: UIImage! = UIImage(data: data!)
                self.downloadedPhotos.append(image) //downloadedPhotos is an array of UIImages

                self.configureFeed() //takes care of some UI

            }
        })
    }

Here I run into the obvious problem: I've only downloaded 10 images, called "1","2",..., "10"

Thank you guys really much for any help!

Upvotes: 0

Views: 809

Answers (1)

Mike McDonald
Mike McDonald

Reputation: 15953

Let's handle these one at a time:

  • What kind of way to name the images when users upload them would you suggest?

I'd take a look at the many other questions like this: Retrieving image from Firebase Storage using Swift

  • How could I keep track of how many images there are?

See above.

  • How could I delete those images from the reference that are older than a week?

You can use Object Lifecycle Management to delete images after a certain time. Deleting them from the database would be harder--maybe an integration with Google Cloud Functions GCS notifications could sync this.

  • Should I use Image Cashing Libraries like Kingfisher or would
    you go with the above style?

I totally recommend using an image loader like SDWebImage or PINRemoteImage after pulling the download image from the realtime database.

Upvotes: 1

Related Questions