Bobby
Bobby

Reputation: 1454

Firebase data ordering

I feel as if I'm missing something big when I use firebase in my android app. I have been using

push();

to put my data in firebase with a unique id. I have been accessing this data in an activity using the ChildEventListener:

ref.addChildEventListener(new ChildEventListener() {         
    // Retrieve new posts as they are added to the database
    @Override public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
        Map<String, Object> newPost = (Map<String, Object>) snapshot.getValue();
    }
});

I believe that

snapshot.getValue()

should provide me with all my unique ids but how do I order them to display them in order from newest to oldest?

Thanks for your help!

Upvotes: 1

Views: 463

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598797

If you call:

ref.push().setValue(anObject);

Then that will fire onChildAdded once, with the data from anObject in snapshot.getValue().

So in this example from the Firebase guide for Android:

Firebase postRef = ref.child("posts");

Map<String, String> post1 = new HashMap<String, String>();
post1.put("author", "gracehop");
post1.put("title", "Announcing COBOL, a New Programming Language");
postRef.push().setValue(post1);

Map<String, String> post2 = new HashMap<String, String>();
post2.put("author", "alanisawesome");
post2.put("title", "The Turing Machine");
postRef.push().setValue(post2);

Let's register our onChildAdded listener:

ref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
        System.out.println(snapshot.getKey());
    }
};

});

The onChildAdded method is called whenever a child is added to the location we register it on. We add two children, so onChildAdded will called twice. The first time it will fire for gracehop the second time for alanisawesome. Each time it will print the key of the corresponding node.

Note: Firebase fires the onChildAdded event also for all existing children at the location. So it fires right away for all existing children, as well as whenever a child is added.

You seem to expect that you get all values at once. If that is what you're looking for, you can accomplish it with a so-called ValueListener

postRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        System.out.println(snapshot.getValue());
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
});

Note that the method is called onDataChange here. So this method is called whenever the data at the location changes. Since you add the children under where postRef, the onDataChange method will be called twice. The first time with just gracehop, the second time with both gracehop and alanisawesome.

You can loop over the nodes in a DataSnapshot with:

@Override
public void onDataChange(DataSnapshot snapshot) {
    for (DataSnapshot postSnapshot: snapshot.getChildren()) {
        System.out.println(postSnapshot.getKey());
    }
}

If we push the two values like before, the onDataChange will fire twice. The first time it will print the key for gracehop the second time it will print the key for gracehop and then the key for alanisawesome. So it will print three values in total, where the snippet with onChildAdded will only print two.

Upvotes: 2

Related Questions