skip to Main Content

I have collection called tasks and each document contains fields like

updatedAt
name
isDone

Instead of querying all docs all the time, I get the latest updatedAt from cache and build the query using greaterThan:latestUpdatedAt. But I feel this logic can horribly go wrong in this scenario.
Consider a user using this app from his phone and web also.

  • Let’s consider current updatedAT on user’s phone is 2023-5-20 08:01:00
  • User creates task T1 on web which immediately synced to server with updatedAt being 2023-5-20 09:01:00
  • User creates a task T2 on phone but at the moment phone has no internet connection
  • When user gets connection back on his phone T2 will sync to server with updatedAt being 2023-5-20 09:02:00

Now the problem is I have no idea how the firestore internal sync logic works so if sync of T2 happens before get tasks query greaterThan:latestUpdatedAt then my latestUpdatedAt becomes 2023-5-20 09:02:00 and I will never get task T1 which is created on web. This is just a speculation and I am not sure about but without the confirmation I am bit skeptical to push the app to production.

This idea of reducing reads is inspired by an article written by firebase expert Alex Mamo but I am not sure whether he is aware of this issue or not.

2

Answers


  1. You have to update latestUpdatedAt when you GET data and not when you PUSH data to Firebase.

    You need a local date to save the last sync and a remote one to track updates, when the remote date is after the local date you have to fetch data from the server and update your local date with the new one.

    with this approach, your scenario is:

    • localUpdateAt on the phone is 2023-5-20 08:01:00 and remoteUpdateAt is 2023-5-20 08:01:00
    • User creates task T1 on the web which immediately synced to the server with remoteUpdatedAt being 2023-5-20 09:01:00
    • User creates a task T2 on the phone but at the moment phone has no internet connection
    • When the user gets connection back on his phone T2 will sync to the server with remoteUpdatedAt being 2023-5-20 09:02:00
    • At this time the mobile app tries to get new data from the server, your localUpdateAt on the phone is still 2023-5-20 08:01:00 that is before remoteUpdatedAt get data from the server with greaterThan:localUpdateAt

    Usually, I create a collection specific to store the last updates for others’ collections.

    I hope can be helpful for you.

    Login or Signup to reply.
  2. How about saving lastSyncTime to local SharePreference only if the connection state of the Firebase is online?

    https://firebase.google.com/docs/database/flutter/offline-capabilities#section-connection-state

    Scenario:

    1. [2023-5-20 07:01:00][phone][online]: phone lastSyncTime=2023-5-20 07:01:00
    2. [2023-5-20 09:01:00][web][online]: web SharePref=09:01:00, same as T1‘s updateAt
    3. [2023-5-20 09:02:00][phone][offline]: phone lastSyncTime no change, T2‘s updateAt is not enable because server is offline
    4. [2023-5-20 09:03:00][phone][online]: check all documents after 2023-5-20 07:01:00, update phonelastSyncTime=2023-5-20 09:03:00, same as T2‘s updateAt
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search