constant vigilance
constant vigilance

Reputation: 79

How do I read the top record in a Firebase Database?

I want to get the top record of the message section of my database, check that the email value doesn't match the users email. If it does, then repeat with the next record down. But if it doesn't match, I want it to populate a textview in my app with its 'message value' and then delete the record from the database.

Here is an example of my write to database code:

 FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

 //setting user info in message class
 message m = new message();
 m.setName(user.getDisplayName().toString());
 m.setMessage(messageEdt.getText().toString());
 m.setEmail(user.getEmail().toString());

 FirebaseDatabase database = FirebaseDatabase.getInstance();
 DatabaseReference myRef = database.getReference("messages").child(uniqueGeneratedMessageid);

 myRef.setValue(m);

That adds it to the data base like so...

enter image description here

I want it to select the top value whatever it may be, without having to reference the specific ygmailcom100.

Upvotes: 0

Views: 787

Answers (1)

Drew Szurko
Drew Szurko

Reputation: 1621

FirebaseDatabase database = FirebaseDatabase.getInstance().getReference();
Query mQuery = database.child("messages").limitToLast(1);

Edit:

It looks like you are trying to load messages from a chat app. If that is the case, I'd recommend you add ChildEventListener instead of a ValueEventListener, the main reason being ChildEventListener will return only the newly added message, where as ValueEventListener will return both the newly added message and the whole database Snapshot each time a new message is added. That will be very costly in terms of efficiency and data usage, wait time, etc.

From what you gave me above, you would implement a ChildEventListener like this:

private final ChildEventListener mChildEventListener;
private final Query mQuery;
private final FirebaseDatabase database;

database = FirebaseDatabase.getInstance().getReference();
mQuery = database.child("messages").limitToLast(1);

ChildEventListener childEventListener = new ChildEventListener() {

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
String value = dataSnapshot.getValue(String.class); 
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}

};

mQuery.addChildEventListener(childEventListener);
mChildEventListener = childEventListener;  

Then in your onDestroy method, you will want to remove the ChildEventListener to avoid memory leak.

public void onDestroy() {
super.onDestroy();
if (mChildEventListener != null) {
  mQuery.removeEventListener(mChildEventListener);
 }
}

Upvotes: 1

Related Questions