George
George

Reputation: 49

Good way firebase database structure in order to get those sorted by dates?

I'm using firebase and making an app which enables users to save 6 profile images.

Now, I have prepared 6 imageViews and user can simply add their images by tapping each imageViews.

enter image description here

I want to save the image to firebase Storage and download URL to Firebase Database. Technically, I can save it but I'm not sure how to struct database and name of image files.

Currently, I named image file with Dateformatter(like "yyyy-MM-dd''HH:mm:ss".jpg) and set download URL to database like "yyyy-MM-dd''HH:mm:ss" : "downloadURL"

The database structure looks like this:

"User" : { 
  “user’s uid” : { 
    "ImgURLs” : { 
      "2018-09-02_21:59:80" : “(here is the url)”, 
      "2018-09-02_22:05:72" : "(here is another url)", 
      "2018-09-02_23:49:54” : "(here is another url)" 
    }, 
  } 
}

But in this way, I dont know how to retrieve datas accordingly for those 6 Imageviews.

Is there anyway to implement this if my question makes sense.

Upvotes: 0

Views: 210

Answers (1)

Jay
Jay

Reputation: 35667

A change to how the URL's are stored should provide an answer

users :
  uid :
    userName: "some name"
    ImgURLs :
       -Jiokoisd094k //key created with childByAutoId
           url : “(here is the url)”
           timestamp : "2018-09-02_21:59:80"
       -k0k4000od0if //key created with childByAutoId
           url : "(here is another url)"
           timestamp : "2018-09-02_22:05:72"

With this, as long as you know the users uid, you can access their image urls at users/uid/ImgURLs. This also allows you to store the timestamp and other information about each image; perhaps a child node of caption: "Dang, you're lookin good" within each child node.

Depending on the use case, if you will need to query those nodes, it may be better to denormalize the data and store them in a separate node

users :
   uid :
      userName: "some name"

urls  :
   uid :
      -Jiokoisd094k //key created with childByAutoId
         url : “(here is the url)”
         timestamp : "2018-09-02_21:59:80"
      -k0k4000od0if //key created with childByAutoId
         url : "(here is another url)"
         timestamp : "2018-09-02_22:05:72"

Upvotes: 1

Related Questions