检索唯一的孩子从Firebase添加到Firebase中的侦听器 [英] Retrieve only childAdded from firebase to my listener in firebase
问题描述
我目前正在运行此python脚本
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.
但是我只特别需要该节点下的新添加的子代.
任何想法我该如何实现??
Any idea how could I achieve that???
推荐答案
Firebase同步数据库状态,它不是消息传递机制.
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.
例如:如果new表示在附加侦听器后添加了数据,则您可能希望在子节点中添加时间戳并对其进行过滤.
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.
或者,您只能请求最近的child_added事件.我不确定如何使用您正在使用的Python库执行此操作,但是在JavaScript中,它将为 firebase.database().ref('accelerometerData/faultyMotorData').orderByKey().limitToLast(1).on('child_added'...
.
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'...
.
另请参阅:
这篇关于检索唯一的孩子从Firebase添加到Firebase中的侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!