Get Random Child from Firebase Database

How does one get a random children of a Firebase node?

Basically, you just have to do enough itr.next() until the iterator is at the nth position (where n is the random number from nextInt()) and then you can simply get the Object you want with getValue(), the example below should show it well:

questionsRef = FirebaseDatabase.getInstance().getReference().child("questions").child("DE");

questionsRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int questionCount = (int) dataSnapshot.getChildrenCount();
int rand = random.nextInt(questionCount);
Iterator itr = dataSnapshot.getChildren().iterator();

for(int i = 0; i < rand; i++) {
itr.next();
}
childSnapshot = (DataSnapshot) itr.next();
question = childSnapshot.getValue(Question.class);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

Retrieve random key from firebase database

I would solve that problem this way:

Create for each list element a value with the field name id or some other you want. It should be a simple integer. Idealy incrementaly growing with the list. You could use the counter to increase the value each time you add an element.

When trying to get a random element from the list first get a random integer between 0 and the total count of all elements. Then use this query to get a single random element:

firebase.database().ref("users").orderByChild("id").startAt(randomNumber).limitToFirst(1);

Even if you don't hit the id with the randomNumber you would the neareas next element. This should be able to scale to any size.

The most diffictult part would be to optain the id incrementaly without repeating them. I would use for that an cloud function that does it when an item is added. Even a combination with the counter would be interesting.

How to retrieve random children from Firebase

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.

How to get a child key name randomly from the Firebase Realtime database with a Javascript cloud function

The following should do the trick:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

function randomKey(obj) {
var keys = Object.keys(obj);
return keys[(keys.length * Math.random()) << 0];
}

exports.onDataAdded = functions.database.ref('/Pickup-Requests/{uid}').onCreate((snapshot, context) => {

const db = admin.database();
const data = snapshot.val();

return db.ref('/Pickers').once('value')
.then(snapshot => {
const pickerUid = randomKey(snapshot.val());
return snapshot.ref.parent.child(pickerUid).set(data);
})

});

I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/



Related Topics



Leave a reply



Submit