Firebase child_added for new items only

前端 未结 5 1194
北恋
北恋 2020-12-15 18:46

I am using Firebase and Node with Redux. I am loading all objects from a key as follows.

firebaseDb.child(\'invites\').on(\'child_added\', snapshot => {
}         


        
5条回答
  •  被撕碎了的回忆
    2020-12-15 18:53

    If your document keys are time based (unix epoch, ISO8601 or the firebase 'push' keys), this approach, similar to the second approach @balthazar proposed, worked well for us:

    const maxDataPoints = 100; 
    const ref = firebase.database().ref("someKey").orderByKey();
    // load the initial data, up to whatever max rows we want
    const initialData = await ref.limitToLast(maxDataPoints).once("value")
    // get the last key of the data we retrieved
    const lastDataPoint = initialDataTimebasedKeys.length > 0 ? initialDataTimebasedKeys[initialDataTimebasedKeys.length - 1].toString() : "0"
    // start listening for additions past this point...
    // this works because we're fetching ordered by key
    // and the key is timebased
    const subscriptionRef = ref.startAt(lastDataPoint + "0");
    const listener = subscriptionRef.on("child_added", async (snapshot) => {
        // do something here
    });
    

提交回复
热议问题