Firebase database - Count child nodes in father nodes [duplicate] - android

I need to measure the size/length of an element or list in Firebase but I don't need the real content.
Something like firebase.database().ref("users/").once("length" || "size").then(callback)
ps Javascript SDK

Firebase doesn't have a count operator, so the only way is to download all children or keep a separate <children>_count property in sync. The latter is not a trivial task (see my answer here for one approach and this example Cloud Function), so most often developers likely end up going with the downloads-too-much-data-but-is-trivial approach:
ref.child("messages").on("value", function(snapshot) {
console.log("There are "+snapshot.numChildren()+" messages");
})
A more efficient way to count the children would be to fire a REST call with shallow=true parameter, which will give you just the keys. See In Firebase, is there a way to get the number of children of a node without loading all the node data?

Also found a post which does it a different way...
Object.keys(users).length;
Post: Length of a JavaScript object

Related

How to see the before and after snapshot of the changed document in Firestore?

Scenario
I am using onSnapshot() to listen to document changes in Firestore. My document contains an array field. I would like to see which items were added/deleted from the nested array in the document. Is there any way to achieve this?
Possible Solution
I see there is an oldDocuments() field in the QuerySnapshot.snapshot object which could be used to achieve my goal. I could just compare the oldDocument with the returned snapshot to see what changed in the nested array. The problem is oldDocument is a private field that I can't access through the API.
I am using Android Kotlin as my client.
There is nothing built in that can help you see what exactly in a document has changed. However, there are two workarounds.
The first one would be to attach a persistent listener and view changes between snapshots. That means that when you attach the listener you get a list of all documents your query returns. As soon as a document is changed, onEvent will fire. This means that you'll be able to know the document that has changed. Knowing that you already have a list with the initial documents, you can find the old document based on the ID and compare it against the new one.
Another solution would be to save a new document each time something changes. This is some kind of document versioning. In this way, you'll always be able to check the new document against the old one using only two document reads.

Kotlin Firebase read child of child data

Hi I have some issue with calling firebase data from the server.
I can call the single child of data but if I call a nasty child of data(ex: multiple branch of child data), It is not working on kotlin side.
I used ".child()" function on Swift side by just adding ".child()" afterward and it worked on IOS side but I do not understand why the kotlin is not working like that.
Does anyone knows how to solve this problem? I've been searching this problem many days but nobody has a clear solutionenter image description here
Here the issue is of synchronous values, basically onDataChange is called on a different thread.. long story short try calling sectionRecyclerView.adapter line inside the for loop that or this will do the trick.

Search for an unknown child key in Android Firebase

Is it possible to add a ValueEventListener by going though an unknown child in firebase for android?
What I've tried so far is something like this:
reference
.child("data")
.child(/*unknown_key*/)
.orderByChild("contacts")
// etc...
Where at the "unknown_key" I've tried many options, such as "{placeholder}", "{*}" or "*"
This is somehow possible in Firebase Functions, by passing a generic node in the URL, such as https://myfirebaseproj.firebase.com/data/{any}/contacts
I know that I could just get the children under the data/ node and do some iterations, but this feels overkill to me, since the node could really be big and the cost is expontential.

How to retrieve random children from Firebase

Suppose we would like to retrieve 15 random children from questions node having this database structured as below:
1. The first (intuitive and discussed) way of retrieving random children from Firebase is to retrieve the whole required parent node (questions as dataSnapshot) and then select some random children on the client-side. This method has been pointed out in many posts, like in this one here .
Obviously, this method has its downsides; for example when querying through a large sized parent node (e.g. over 10.000 children) retrieving such an amount every time would result in a huge bandwidth usage as well as a client side burden. (when we actually require only a small amount of children)
2. Moving on: another approach, as described here which uses an iterator somehow bypasses the whole client side burden, yet the huge bandwidth usage could still occur as we download the whole parent node every time.
3. An interesting approach is described in Tom's answer in this firebase discussion which proposes:
A hacky way of doing this would be to generate a random key and do a query with startAt().limit(1). I have a feeling this could hurt the performance of your firebase though, so this should not be an operation you perform often. We don't have a real random sample function.
This solution actually sounds pretty good, yet I am not sure how it would indeed impact my Firebase.
4. Another silly solution could actually be naming the question ids manually, so to speak, from 0 to N, therefore handling the random group of ids on the client side and retrieving the questions spot-on by knowing the actual name of nodes.
5. And lastly, I have come up with the following solution to which I ask if is more or less viable than the ones presented above: creating another parent containing the question ids only and when needed, one should retrieve this parent which is much "lighter" than questions parent . From there, I would have the specific random ids and I would only need to snipe for those children. To better understand my meaning, please check the below picture:
Now, from this method arises the following issue: is assigning (let's say) 15 eventListeners good practice? Could this actually slow up things? (Note: this applies to methods 3 and 4 as well)
And ultimately, which method is actually the optimal one when querying from a large database for some random children?
You can use the classic solution as I explained in this answer but if you are afraid of getting huge amount of data then use instead 15 listeners. There is nothing wrong in using listeners as long as you remove them according to the life-cycle of your activity. So, IMHO go ahead with 15 listeners.
We have 2 case here
case 1
If you want to grab all details of the random ids at once, then I suggest 1 listener to the parent node (get value of datasnapshot using pojo class).
case 2
If you want to get the details independently upon request then you will have to attach a listener to each (random id) that you want.
Concerning performance
Try to use only Listener For Single Value Events as they listen one time and then stop (better for performance).
Dont use Value Event Listener (because these listeners keep checking for changes and therefore bad performance as listeners increase).
EDIT
lets say you listened to (questions_ids) node, now you have access to the random id keys, store them in a String variable, and then inside the same listener add another listener to (questions) pointing to the id that you want to grab details
//first listen to question ids ref (the one with 15 ids)
question_ids_ref.addListenerForSingleValueEvent(...{
//grab the key of each (random id) and store in variable
String random_01=......;
//run another listener this time to questions ref
questions_ref.child(random_01).addListenerForSingleValueEvent(..{
//get details of random_01 and so on....
});
});

SimpleDB - how long after insert until item is available to be read?

I have a Fragment, and once the user presses OK, an Item is added to my database and its ID is added to the ArrayAdapter. Immediately after, the adapter tries to draw the view, but the first time it tries to get its attributes, it returns a null HashMap (it gets drawn properly the following times).
Is there a way to make sure the item is in the table before trying to get its attributes?
Even putting the attribute retrieval into a while loop until it returns a not-null HashMap doesn't work so it doesn't look to be an issue of time.
You need to do Select or GetAttributes with ConsistentRead=true as Amazon SimpleDB supports two read consistency options: eventually consistent read and consistent read. Eventually consistent read is default. For more detail please refer doc. link
Try using AsynTask.
Add item to database in doInBackground.
Read it in postExecute.
You are done.

Categories

Resources