0

I am writing an Android app that retrieves its data (in this case Questions) from Firebase' Real-Time database.

When the app starts I want to retrieve X random questions from the DB. However I do not want to fetch all of the questions and take random ones in the client side.

I know that I can limit the number of entries to fetch but they wont be random. I tried to see if I can write a server side to get the random data but I don't see how I can do it. I would write a PHP script in the server but It seems that there are only Cloud Functions.

Any suggestions ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Blondy314
  • 751
  • 9
  • 24
  • I have a few ideas, most of which do involve cloud functions. However, before I start looking into an approach, I need a few more details. First, have you deployed cloud functions before? If not, you should perform some tutorials or watch the [Firebase](https://www.youtube.com/user/Firebase/playlists) cloud function videos on youtube. Second, I need to know more about the randomness. Do you want truly random, or do you want a random shuffle (see everything once, but in random order)? – James Poag Sep 16 '18 at 14:10
  • 1
    Dan McGrath wrote a great answer a while ago on how to select a random document from Cloud Firestore: https://stackoverflow.com/questions/46798981/firestore-how-to-get-random-documents-in-a-collection. While Realtime Database uses a different data model, the same approach could work there. – Frank van Puffelen Sep 16 '18 at 14:24
  • And for a slightly different version from what Frank linked you can take a look at [my answer on this question](https://stackoverflow.com/q/35524816/4916627) that is about getting random items from the realtime database. – André Kool Sep 16 '18 at 21:57

0 Answers0