Home > Back-end >  Distinguish between local cache callback and server reached callback in Firebase
Distinguish between local cache callback and server reached callback in Firebase

Time:05-05

I was building a chat application in Android using Firebase. When we use Firestore SDK, it triggers any listeners on updates as soon as data is written into local Firestore cache. Since I have included server-time in the field, a second trigger will occur when the data has actually reached server, since server-time gets updated there. Is there a possible way for me to distinguish between these two triggers, so that, I can update the user that the message reached the server (like WhatsApp).

If this is absolutely not possible, I have two options:

  1. Use custom cloud functions to send the message, so that listener triggers only after data has reached the server. Until then, I can show pending status for user.
  2. Use custom cloud functions that triggers on Firestore writes at that location, and sets a flag, which indicates the data has reached the server.

If I have choose, which is better?

CodePudding user response:

You can determine whether a snapshot is guaranteed to be up-to-date with the server by checking the isFromCache() and hasPendingWrites() methods in its metadata.

  • Related