简体   繁体   English

Flutter Firebase 每 24 小时刷新一次文档快照

[英]Flutter Firebase refresh document snapshot every 24h

I have a recipe collection with 100 entries in firebase firestore.我在 firebase 火库中有一个包含 100 个条目的食谱集合。 The idea is that the user only sees 3 different recipes in my app.这个想法是用户在我的应用程序中只能看到 3 个不同的食谱。 Those 3 recipes need to change every 24h.这 3 个食谱需要每 24 小时更换一次。 So on Monday the user sees 3 (different) recipes from my collection, on Tuesday the next 3 (randomly chosen) recipes and so on.因此,周一用户会从我的收藏中看到 3 个(不同的)食谱,周二是接下来的 3 个(随机选择的)食谱,依此类推。 How is that possible to achieve?这怎么可能实现?

Simply store another field called ' lastRefresh ' in a document for each user.只需在每个用户的文档中存储另一个名为“ lastRefresh ”的字段。

The value of that field will be a DateTime , of course you have to format it to a string with DateTime().toIso8601String() to store it on Firestore.该字段的值将是DateTime ,当然您必须使用DateTime().toIso8601String()将其格式化为字符串才能将其存储在 Firestore 上。

Then on app start get the value and parse it to DateTime again with DateTime.parse(value_from_firestore) and get the difference to now.然后在应用程序开始获取值并使用DateTime.parse(value_from_firestore)再次将其解析为DateTime并获取到现在的差异。 You can achieve this by calling DateTime.now().difference(parsed_date_time_from_firestore) which will return a duration.您可以通过调用DateTime.now().difference(parsed_date_time_from_firestore)来实现这一点,这将返回一个持续时间。

You can check wheter this duration is greater then 24 hours.您可以检查此持续时间是否大于 24 小时。 If it is, load another 3 recipes from firestore and set lastRefresh with DateTime.now().toIso8601String() to now again.如果是,请从 firestore 加载另外 3 个配方,并使用DateTime.now().toIso8601String()再次将 lastRefresh 设置为 now。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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