0

I am using Firebase as follow:

Post--

    -Kwc6asRRI1SUqrigYeD                 <- First input
                       -> Date: 1:00pm
                       -> ID: 1
                       -> Content: Hello!
    -Kwc6fXQsN2xIQtHOofZ  
                       -> Date: 2:00pm
                       -> ID: 2
                       -> Content: How are you?
    -Kwc6fXQsN2xRO39LDPD                  <-Most recent one
                       -> Date: 3:00pm
                       -> ID: 3
                       -> Content: I am good.

These are "pushed" thus an unique key is generated which can be used to display them according to the "most recent to old" (or ID:3 to ID:1).

Now, when I need to update a data, let say ID:1 post's content from "Hello" to "My name is Steve", then it still maintains the unique key even though this is the most recent one.

Post--

    -Kwc6asRRI1SUqrigYeD                 <-Most recent one
                       -> Date: 4:00pm
                       -> ID: 1
                       -> Content: My name is Steve
    -Kwc6fXQsN2xIQtHOofZ  
                       -> Date: 2:00pm
                       -> ID: 2
                       -> Content: How are you?
    -Kwc6fXQsN2xRO39LDPD                  
                       -> Date: 3:00pm
                       -> ID: 3
                       -> Content: I am good.

I guess I can delete the post and set a new one, but that seems inefficient especially if I have more data on each child.

Is there a way to re-set the key so that it reflects the time change (like below)?

Post--

    -Kwc6fXQsN2xIQtHOofZ  
                       -> Date: 2:00pm
                       -> ID: 2
                       -> Content: How are you?
    -Kwc6fXQsN2xRO39LDPD                  
                       -> Date: 3:00pm
                       -> ID: 3
                       -> Content: I am good.

    -Kwc6asRRI1KDodkeixk                <-Most recent one
                       -> Date: 4:00pm
                       -> ID: 1
                       -> Content: My name is Steve
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Steve Kim
  • 5,293
  • 16
  • 54
  • 99

1 Answers1

2

There is no way to update the key of an existing node (see 1, 2, 3). If you want a new key, you'll have to generate a new node with the same content.

But in this case it seems much more likely that you want to keep your data structure as is and instead add a lastUpdated timestamp to each post:

Post--
    -Kwc6asRRI1SUqrigYeD
                   -> Date: 1:00pm
                   -> lastUpdated: 1508215054096
                   -> ID: 1
                   -> Content: Hello!
    -Kwc6fXQsN2xIQtHOofZ  
                   -> Date: 2:00pm
                   -> lastUpdated: 1507610306270
                   -> ID: 2
                   -> Content: How are you?
    -Kwc6fXQsN2xRO39LDPD
                   -> Date: 3:00pm
                   -> lastUpdated: 1508128668412
                   -> ID: 3
                   -> Content: I am good.

With this structure you can use a Firebase query to get the results in the order you want. In JavaScript this would look something like:

var ref = firebase.database().reference("Post");
ref.orderByChild("lastUpdated")
   .once("value")
   .then(function(snapshot) {
     snapshot.forEach(function(post) {
       console.log(snapshot.key+": "+snapshot.val().Content);
     });
   });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807