Retrieve only childAdded from firebase to my listener in firebase

允我心安 提交于 2021-01-29 02:45:28

问题


I currently have this python script running

# Setup Firebase Listner

# Retrieve the data from Firebase and do the analysis

# Append the data to Firebase

from firebase_streaming import Firebase

# Firebase object
fb = Firebase('https:***************.firebaseio.com/')


# Callback function to Analyze the data for a Faulty Motor
def performAnalysis_FaultyMotor(x):
    print(x)

def performAnalysis_motor(x):
    print(x)

# Declaring Custom callback definitions for the faulty and motor data
custom_callback_faultyMotor = fb.child("accelerometerData/faultyMotorData").listener(performAnalysis_FaultyMotor)
custom_callback_motor = fb.child("accelerometerData/motorData").listener(performAnalysis_motor)


# Start and stop the stream using the following
custom_callback_faultyMotor.start()
custom_callback_motor.start()

which is returning me the entire data under the respective node that I have my listener attached to.

But I require specifically only the newly added child under that node.

Any idea how could I achieve that???


回答1:


Firebase synchronizes database state, it is not a message passing mechanism.

This means that if you attach a listener on a node, you get all data under that node. If you only want new data, you'll have to define what "new" means.

For example: if new means data added after you attach a listener, then you may want to add a timestamp in your child nodes and filter on that.

Alternatively, you can request only the most recent child_added event. I'm not sure how to do that with the Python library you're using, but in JavaScript it would be firebase.database().ref('accelerometerData/faultyMotorData').orderByKey().limitToLast(1).on('child_added'....

Also see:

  • How to retrieve only new data?
  • How to only get new data without existing data from a Firebase?
  • How to get only the new items added to firebase?
  • Firebase child_added only get child added


来源:https://stackoverflow.com/questions/53303695/retrieve-only-childadded-from-firebase-to-my-listener-in-firebase

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