简体   繁体   中英

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 don't know how many images there are in my Firebase reference folder
  • I want only the images to be shown that have been taken, let's say in the last week

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"

  • What kind of way to name the images when users upload them would you suggest?
  • How could I keep track of how many images there are?
  • How could I delete those images from the reference that are older than a week?
  • Should I use Image Cashing Libraries like Kingfisher or would you go with the above style?

Thank you guys really much for any help!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM