How to only get new data without existing data from a Firebase?

后端 未结 4 931
无人及你
无人及你 2020-11-27 23:16

I have a node in Firebase getting continually updated with information from a logfile. The node is lines/ and each child of lines/ is from a

相关标签:
4条回答
  • 2020-11-27 23:27

    You need to include a timestamp property and run a query.

    // Get the current timestamp
    var now = new Date().getTime();
    // Create a query that orders by the timestamp
    var query = ref.orderByChild('timestamp').startAt(now);
    // Listen for the new children added from that point in time
    query.on('child_added', function (snap) { 
      console.log(snap.val()
    });
    
    // When you add this new item it will fire off the query above
    ref.push({ 
      title: "hello", 
      timestamp: Firebase.ServerValue.TIMESTAMP 
    });
    

    The Firebase SDK has methods for ordering, orderByChild() and methods for creating a range startAt(). When you combine the two you can limit what comes back from Firebase.

    0 讨论(0)
  • 2020-11-27 23:34

    You have the right idea. child_added should be called only for the new nodes. Without source code it's hard to tell why you get all the data in your child_added event.

    You can check the chat demo app to see how they load new chat messages. The use case sounds similar.

    https://github.com/firebase/firechat/blob/master/src/js/firechat.js#L347

    0 讨论(0)
  • 2020-11-27 23:36

    Here's temporary but quick solution:

    // define a boolean
    var bool = false;
    
    // fetch the last child nodes from firebase database
    ref.limitToLast(1).on("child_added", function(snap) {
        if (bool) {
            // all the existing child nodes are restricted to enter this area
            doSomething(snap.val())
        } else {
            // set the boolean true to doSomething with newly added child nodes
            bool = true;
        }
    });
    

    Disadvantage: It will load all the child nodes.

    Advantage: It will not process existing child nodes but just the newly added child nodes.

    limitToLast(1) will do the work.

    0 讨论(0)
  • 2020-11-27 23:46

    I think there is a problem in @David East's solution. He is using the local timestamp which may cause problem if the time is not accurate in client device. Here is my suggested solution (iOS Swift):

    • Using observeSingleEvent to get the complete data set
    • Then returned it in reversed order by reversed()
    • Get the last timestamp by for example data[0].timestamp
    • Using queryStarting for timestamp

       self._dbref.queryOrdered(byChild: "timestamp").queryStarting(atValue: timestamp+1)
           .observe(.childAdded, with: {
              snapshot in
              print(snapshot.value)
        })
      
    0 讨论(0)
提交回复
热议问题