Firestore — Get only the changed documents in a large synced collection

冷暖自知 提交于 2020-06-22 04:25:06

问题


I've read all of the questions below, and cannot find anything in the docs to describe how to sync a collection and receive only changed documents from a collection. I've got over 500 documents in my synced collection (using redux-saga-firebase syncCollection) but usually only about 100 of them ever change. At any given time, even fewer will change, but I'm currently getting all 500+ documents back when even one changes, which results in 500+ reads. Not ideal, and will cost me at scale.

Here is my code using redux-saga-firebase, but a vanilla firebase code answer would also suffice. How can get a synchronized response that sends only the changed documents in the collection?

export function* getPlayersFromDb() {
  yield fork(
    rsf.firestore.syncCollection,
    'players',
    { successActionCreator: response => ({
            type: t.GET_PLAYERS_FROM_DB_SUCCESS,
            payload: [...response],
        }),
        failureActionCreator: () => ({
            type: t.GET_PLAYERS_FROM_DB_FAILURE,
            payload: [],
        }),
        transform: response => messageTransformer(response),
    }
);

}

Does Firestore sync only diff?

Realm syncing with large collection on Firestore - architectural questions / issues

How to avoid unnecessary Firestore reads with Cache

What exactly does Firestore synchronization do?


回答1:


Firestore queries don't have a notion of querying "only things that have changed". You will need to create that notion on your own. The usual way to do this is by recording a timestamp in each document with the time it was last updated, then using that field in a range filter to get only documents updated since the latest update was received. You will need to track that latest update timestamp in the client. One possibility is to use the greatest timestamp seen in any document to date.



来源:https://stackoverflow.com/questions/62314708/firestore-get-only-the-changed-documents-in-a-large-synced-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!