Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 787 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Firebase android count children and store it in a variable

#1
In my application I'm able to get children count by datasnapshot.getchildren().
But what should I do if I want to store that count in a variable?

mRefReg.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.i(dataSnapshot.getKey(),dataSnapshot.getChildrenCount()+"Count");

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

including "mCount = dataSnapshot.getChildren" does not seem to work.
Reply

#2
There's some info missing in your question, but I think I know what's going on.
Listeners in Firebase are asynchronous, so you cannot set a variable that easy.

Options to go:

1) Declare your variable inside the onDataChange method.

2) Only use the variable when onDataChange method has completed.

3) Pass a callback function in the method where you have declared your listener.
Reply

#3
What you're seeing is likely the fact that data from Firebase is loaded asynchronously. The first time you encounter this, it's a big paradigm shift. But since most modern cloud APIs work this way, it's best to embrace it as quickly as possible.

The easiest way I've found to see what happens when we place some logging statements:

System.out.println("Before adding listener");
mRefReg.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("In onDataChange");
}
public void onCancelled(DatabaseError databaseError) { }
});
System.out.println("After adding listener");

Contrary to what your intuition tells you, the output of this will be:

> Before adding listener
>
> After adding listener
>
> In onDataChange

The reason for this is (as I said at the start) that Firebase asynchronously gets the data from the database. A fun way to realize why that is, is to make a change to your data (for example in your [Firebase Database console](

[To see links please register here]

)). When you make this change, the `onDataChange` will be invoked again and print another:

> In onDataChange

Unfortunately this means that you cannot do the following to handle the children count:

int count = -1;
System.out.println("Before adding listener, count="+count);
mRefReg.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
count = dataSnapshot.getChildrenCount();
System.out.println("In onDataChange, count="+count);
}
public void onCancelled(DatabaseError databaseError) { }
});
System.out.println("After adding listener, count="+count);

Because the output will be:

> Before adding listener: count=-1
>
> After adding listener: count=-1
>
> In onDataChange: count=3 (assuming you have 3 children)

The best trick I've found to deal with this asynchronous loading is to reframe the problem. Instead of saying "first we get the count, then we do xyz with it", try framing it as "whenever we get the count, do xyz with it". This means that you need to move the code that requires the count *into* `onDataChange()`:

mRefReg.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
int count = dataSnapshot.getChildrenCount();
// TODO: show the count in the UI
}
public void onCancelled(DatabaseError databaseError) { }
});

If you implement it like this, the count in the UI will be updated whenever the number of items in the database changes.
Reply

#4
long ct = 0;

public void Count() {
databaseReference.child("a").child("b").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
ct = dataSnapshot.getChildrenCount()`enter code here`;
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

}

Call this function at the beginning and it will keep updating the variable ct whenever the child count increase or decreases in the firebase database.
Hope it helps
Reply

#5
Just do this. The function `datasnapshot.getChildrenCount` returns long type value so you need to cast it to integer first.

``` java
mRefReg.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mCount = (int) dataSnapshot.getChildrenCount;
Log.i(dataSnapshot.getKey(), mCount +""`enter code here`);

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through