0

In my game, I allow users to create a level and upload it to my firebase storage. I also get the download link and upload the URL in a firebase database under a LevelsURL node.

I then want to get a random URL from the database, but I am unable to find the best way to do so as all solutions I found suggested getting the entire node of URLs and choosing a random URL on the client-side.

While this solution technically works, I am concerned if I have a large list of user-created levels as potentially I could have hundreds or thousands of levels and I'm unsure about the performance to download a large list of URLs and the amount of data I have will have to download.

Any suggestions on the best approach to get a random file from firebase storage?

Smaika
  • 1,187
  • 4
  • 12
  • 20
  • It's not clear what the issue is doing this on the client side, or why you would download thousands of levels. If there are 100 levels (named level_000, level_001, level 002 etc), generate a random number between 0 and 99 and download that level, no? Also, there are a LOT of questions and solutions here on SO about getting random data from Firebase (Realtime Database, Firestore, Storage). – Jay Apr 10 '22 at 13:11
  • No, How can I know the number of levels available, also how can I ensure the correct level name if users deleted the file. Currently I store levels under root-->Usres-->UserID-->CustomLevels-->"UniqueLevelname" I then get the download URL and store it in database under root-->CustomLevelsURL-->UserID-->"UniqueLevelname" : url – Smaika Apr 10 '22 at 21:41
  • Are you asking how to count the number of documents in a collection? If so, try [this](https://stackoverflow.com/questions/46554091/cloud-firestore-collection-count) and [this](https://stackoverflow.com/questions/46553314/how-to-get-a-count-of-number-of-documents-in-a-collection-with-cloud-firestore) and maybe even [this](https://stackoverflow.com/questions/70184560/getting-number-documents-of-a-collection-in-firebase-in-dart-flutter/70340004#70340004). The level names would come from Firestore and if one is deleted, you simply update the indexes in firestore. – Jay Apr 11 '22 at 17:18

0 Answers0