Stop getting Real Time update on Firebase Database Reference - android

I know firebase database is a real time database but for my feature I don't want to update a specific database reference (below reference) value in real time. So, is there any way to stop getting real time update on firebase database reference?
mUserDataRef = FirebaseDatabase.getInstance().getReference().child("users");

The code you shared only creates a reference to a location in the database. It does not start getting realtime updates yet. To start getting realtime update, attach a listener with addValueEventListener or addChildEventListener. An example of the latter:
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
};
mUserDataRef.addChildEventListener(childEventListener);
This will receive data updates until the listener it removed. To remove the listener, call:
mUserDataRef.removeEventListener(childEventListener);
If you only want to get data once, instead of receiving continuous updates, you can call addListenerForSingleValueEvent:
mUserDataRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
Log.d(TAG, "onDataChange:" + dataSnapshot.getKey());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});
I highly recommend studying these two pages in the Firebase documentation:
Read and write data on Android
Work with lists of data

Related

How to get the list of child nodes from firebase database reference?

Below I've attached the picture of DB Structure and I'm trying to get the list of child nodes from firebase DB reference (timingInformation). How can I get the list?
To read/synchronize data from the Firebase Database, you'll need to attach a listener. A simple case:
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("timingInformation");
ref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
};
For more see the Firebase documentation on loading data from a list, and the reference documentation on what you can do with a snapshot.

Firebase datasnapshot.getValue() returns null

In my app, I have used Firebase database and stored userID in a child and set the value of the child as username to get the username of the current user. Now I'm using addValueEventListener to get the username from the database. This is my Firebase structure.
The code is given below.
checkUsername.child("check").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
currentUser = dataSnapshot.child(getUserID()).getValue(String.class);
Log.d(TAG, "onDataChange: currentUser = " + currentUser);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
While debugging, String currentUser returns null.
Screenshot of debugged answer.
Since you have added the valueEventListener at the check node, so getting the child again makes no sense (Because the User ID is the key of check node and not a separate child).
Instead try calling dataSnapshot.getChildren(); directly to get the entire list of updated data and iterate through it to get whatever key value you need.
Something like this,
checkUsername.child("check").addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
String current value = dataSnapshot.getValue(String.class);
Log.d("TAG", "onDataChange: " + current value);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}
#Override
public void onCancelled(DatabaseError databaseError) {}
});
Another option in this particular use case is to add a ChildEventListener and get the value inside onChildAdded() method.

Callback in Firebase android

After create data on Firebase. I try retrieving data from Firebase. But I have problem, I think may be Log.d(TAG,list.size()) run before ref.addChildEventListener(childEventListener); complete. Who can help me ?
public class NewFirebase extends AppCompatActivity {
List < Product > list = new ArrayList < > ();
private static final String TAG = "Firebase";
DatabaseReference ref;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
ref = FirebaseDatabase.getInstance().getReference();
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
// A new comment has been added, add it to the displayed list
Product comment = dataSnapshot.getValue(Product.class);
for (DataSnapshot child: dataSnapshot.getChildren()) {
Product post = child.getValue(Product.class);
list.add(post);
}
// ...
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "postComments:onCancelled", databaseError.toException());
}
};
ref.addChildEventListener(childEventListener);
Log.d(TAG, list.size() + "");
}
class RetrievingData extends AsyncTask < Void, Void, Void > {
#Override
protected Void doInBackground(Void...voids) {
return null;
}
}
}
You need to take a second approach to how you are structuring your code, or even take a look at the definition of callback/listener itself.
The addChildEventListener() method assigns a callback and initiates a query for retrieving the result. That is, of course, done in background.
Using listeners will never work that way, that's why they were made for, to don't follow line-by-line execution. If you want to get some result from them, you need to put the code inside their methods, which is when they give you some response. Can take milliseconds, seconds, even minutes, but don't expect to be so immediate to be quicker than the execution of the next line that it was posted to execution.
Take a look at https://www.firebase.com/docs/android/guide/retrieving-data.html.
If you want to see the size of the list that you get from Firebase database, you should use addValueEventListener instead of addChildEventListener
List<Product> commentList = new ArrayList<>();
myRef.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot: snapshot.getChildren()) {
Product comment = postSnapshot.getValue(Product.class);
commentList.add(comment);
}
// here you can print the size of your list
Log.d(TAG,list.size())
}
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});

firebase android having trouble retrieving a list of children at a specific location

I am trying to retrieve a list of children at a specific location, but I get only gibberish when I do what I think is right.
My code for retrieving data:
final String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabase = FirebaseDatabase.getInstance().getReference();
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
adapter.add(dataSnapshot.child(userId).getChildren().toString());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
adapter.add(dataSnapshot.child(userId).getChildren().toString());
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
adapter.add(dataSnapshot.child(userId).getChildren().toString());
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "postComments:onCancelled", databaseError.toException());
Toast.makeText(getActivity(), "Failed to load entries.",
Toast.LENGTH_SHORT).show();
}
};
mDatabase.addChildEventListener(childEventListener);
Screenshot of datastructure
What I am trying to retrieve a list of the children below the userId.
What am I doing wrong?
I don't think you should add the ChildEventListener to the root node of your firebase database since this would download all the data in your database.
Instead, call it on the specific user since you only want it for that UID.
final String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabase = FirebaseDatabase.getInstance().getReference(userId);
ChildEventListener childEventListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
// maybe use a POJO here to collect data easily
// the toString() method is maybe for testing??
adapter.add(dataSnapshot.getValue().toString());
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
// This part should have appropriate code
// Unless of course you actually want to add it
//to the adapter everytime
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
// This part should have appropriate code
// Unless of course you actually want to add it
//to the adapter everytime
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "postComments:onCancelled", databaseError.toException());
Toast.makeText(getActivity(), "Failed to load entries.",
Toast.LENGTH_SHORT).show();
}
};
mDatabase.addChildEventListener(childEventListener);

FireBase OnChildAdded not called on Android

I'm using OnChildAdded event for get all data from database on Android.
The first time and many time later, It works well.
But something, onChildAdded don't be call for old child anymore.
Uninstall and install app again make it work again.
I checked permission and added event.
I don't know what's wrong and what have to do to fix it.
This code, I use for init
prayersRef = database.child("prayers");
Log.w(TAG, "FireBaseDatabaseManager");
prayersListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Prayer prayer = dataSnapshot.getValue(Prayer.class);
prayer.uid = dataSnapshot.getKey();
Log.w(TAG, "onChildAdded:" + prayer.uid);
if (delegate != null) {
delegate.OnPrayerAdded(prayer);
}
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.w(TAG, "onChildChanged:" + dataSnapshot.getKey());
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.w(TAG, "onChildRemoved:" + dataSnapshot.getKey());
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.w(TAG, "onChildMoved:" + dataSnapshot.getKey());
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
};
Then I add event in here
Log.i(TAG, "Add Child Event Listener");
prayersRef.addChildEventListener(prayersListener);
OnChildAdded event is used to refreshing data. Try to retrieve data using ListenerForSingleValueEvent :) then in onDataChange you can put all downloaded items in list :) like:
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Model> models = new ArrayList<>();
for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
Model model = snapshot.getValue(Model.class);
models.add(model);
}
}

Categories

Resources