Reputation: 103
I wanted to create an application that uses firebase as a backend.Now the problem I have is that I have to attach a listener to get back a snapshot of data. But for every time my application starts,I want to query firebase for data and populate my views even though there has been no change in the database.
Upvotes: 1
Views: 4119
Reputation: 3966
I suppose you are doing something like this:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Code
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Code
}
});
So for what I think you're asking, calling an alternative method called addListenerForSingleValueEvent
should solve the problem. It will not listen for changes, as soon as returns a value, it will stop connecting until being attached again.
Result
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Code
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Code
}
});
Upvotes: 7