Reputation: 434
How can i implement a listener on firebase to keep check if data has changed in firebase? for example, a user insert a data in firebase, a second user is checking a listview of that data, i want to refresh that listview automatically.
Upvotes: 0
Views: 69
Reputation: 3313
You need to listen to Firebase Database changes, and once data added to the database, you will be notified on client side and you will receive the data that is added to firebase database because it's realtime database.
mFirebaseDatabase.child("yourNode").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
// here you need to handle the value added.
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 2
Reputation: 1310
There are two ways.
Way 1: If possible, create a callback and after user insert a new item, you callback will fire.
Way 2: Every N second (2-5), you could make a request to server to ask him if new data avaliable.
Also if you are building a list with recyclerView, for efficient updating list, use DiffUtil. Which case is better, you decide. I don't work with firebase list.
Upvotes: 2