0

I am working on an Android application that sharing the ideas. My app uses Firebase to store the ideas and when user open the app, I want to get randomly N ideas from Firebase. How can I implement feature getting randomly N items from Firebase?

Anh Vu
  • 219
  • 3
  • 10
  • Your question's answer solved. https://stackoverflow.com/questions/28154966/get-random-value-android-firebase-java – Yusuf Çakal Aug 14 '17 at 08:36
  • Your question's solved. https://stackoverflow.com/questions/28154966/get-random-value-android-firebase-java – Yusuf Çakal Aug 14 '17 at 08:38
  • See https://stackoverflow.com/questions/40853157/firebase-random-query or (for iOS, but equally applicable to Android) https://stackoverflow.com/questions/40765613/get-random-child-from-firebase-database – Frank van Puffelen Aug 14 '17 at 14:14

2 Answers2

1

I don't think there is any option to get randomly data from one node . But u can retrieve all data from firebase database then show random data by programmatically.

  • My data is really big and I don't know is a good idea for getting all data first. Anw, Thanks for answer :) – Anh Vu Aug 14 '17 at 08:33
  • can u share your database design ... – Shahadat Hossain Shaki Aug 14 '17 at 08:34
  • The Idea item is not complex but the number of items is quite big. For example, if I have > 10.000 items, is this ok to get all data? – Anh Vu Aug 14 '17 at 08:37
  • U can categorize those data and save them in multiple node. then when app open, u randomly select category and from those randomly pick some value ... Or u can save value with ur known url. then u can hit specific url to get data ... pushid create a random value to save data here u cant use pushid to save ur data. u can use array index for save ur data and randomly hit from ur known url . – Shahadat Hossain Shaki Aug 14 '17 at 09:05
0

One solution is to create compound keys for the idea keys, essentially an index with a string plus a number

ideas
  idea_0
    idea: "The Wheel"
  idea_1
    idea: "Internal Combustion Engine"
  idea_2
    idea: "Electricity"
  .
  .
  .
  idea_X
    idea: "Disco"

Then, in code, generate a random number from 0 to X... Then append that random number to the string and load just that node from Firebase.

So if we generated random number 2, the key would be

idea_2

and now you have electricity! Be sure to make the random number generator inclusive so you get numbers from 0 to X; you wouldn't want to omit the idea of 'Disco', right?

Do that three times in a loop for example and you will have three random ideas.

Jay
  • 34,438
  • 18
  • 52
  • 81