0

I have an Android app and I need make Firestore subscription to get docs created during the last hour. I have created-timestamp field in all docs. So the subscription should work like this: if docs stops to be created, over the time result bundle should contains less and less docs (filtering only the last hour relatively current time). What is the best way to do this? Thanks.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Konstantin Konopko
  • 5,229
  • 4
  • 36
  • 62

2 Answers2

1

You cannot use the onSnapshot() method to listen to the collection because the docs in your collections do not change: After receiving the initial query snapshot the snapshot handler will not receive any new snapshot (unless another change occurs in one of the documents of course)

One solution is to fetch the collection every minute. This can be costly.

Another approach would be to use a Cloud Function to update a specific flag in the docs one hour after creation and use onSnapshot() because in this case the documents do get updated!

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Obviously I could be calling for docs regularly, but still trying to figure out some elegant solution with minimum of code. Cloud function, as you suggested, also shoud be auto-called each minute, it looks not optimal. Maybe Google Cloud team can help. – Konstantin Konopko Jun 08 '23 at 13:04
  • No the Cloud Function does not need to be called every minute. Please read the Medium article linked in my answer. – Renaud Tarnec Jun 08 '23 at 13:07
  • Thanks, that is great article from Doug. But it looks like Cloud Tasks is not appropriate for my case, because I need to get actual last-hour docs in (almost) realtime mode. – Konstantin Konopko Jun 08 '23 at 13:19
  • By the way, I guess it is a good point for Cloud team to think about time-relative subscriptions. It could substantially inpower Firestore query system, imho. – Konstantin Konopko Jun 08 '23 at 13:27
  • I do think the technique described by Doug will fullfill your functional needs. – Renaud Tarnec Jun 08 '23 at 13:29
  • 1
    Got it! If I apply Cloud Task scheduler to each doc when it was created to set an hour-expired flag after hour — it looks like good solution. Thanks Renaud! – Konstantin Konopko Jun 08 '23 at 13:37
0

There is no way to make the timestamp in the query dynamic, so you will have to remove the expired documents from the results in your application code.

This takes two steps:

  1. Start a query for the documents created in the past hour
  2. Continuously remove the expired documents in your application code

The query itself should be quite simple: calculate the timestamp of one hour ago, and add a realtime listener on documents with a creation timestamp greater or equal to that.

When you start the query this will give you the correct documents, and it will give you the new documents. But since the timestamp in the query is fixed, it won't remove the outdated documents.

Then you'll have to run a local process periodically (say every few seconds or so) to check for expired documents in the results you got last.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807