简体   繁体   中英

Firebase Cloud functions - read limited amount of document snapshots every 24h

I have a collection of 200 recipes on Firebase Firestore. In my app (I am using flutter) I want the user to only see 3 randomly chosen recipes (all user will see the same 3 recipes). These 3 recipes should change every 24h. So on Monday my user see 3 recipes, on Tuesday another 3 and so on.

I stumbled upon firebase cloud functions. I think this could be the right thing. But before I upgrade to a Blaze plan I would like to know if I can achieve this with cloud functions and if yes, does anyone know how? I only find a few single tutorials here and there for cloud functions.

You can use Firebase Scheduled Cloud Functions that'll run every 24 hours (maybe every midnight) and fetch random documents from Firestore and save the contents to a separate collection (eg daily_recipes) and then query this collection from client. Make sure you delete the documents selected on the previous day.

You can just store all random recipes in a single documents so it'll only 1 read to fetch all recipes at once or alternatively use the realtime database to store the recipes if the data will be access frequently.

If you want to run the Cloud function less frequently (eg 7 days), then you can fetch 21 documents and store a field date in them. Then you just query recipes by today's date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM