我目前正在运行这个 python 脚本
# 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()
它返回我附加了监听器的相应节点下的全部数据。
但我只需要该节点下新添加的子节点。
知道我怎样才能实现这一目标吗???
最佳答案
Firebase 同步数据库状态,它不是消息传递机制。
这意味着如果您在节点上附加监听器,您将获得该节点下的所有数据。如果您只想要新数据,则必须定义"new"的含义。
例如:如果 new 表示在附加监听器后添加的数据,那么您可能需要在子节点中添加时间戳并对其进行过滤。
或者,您可以仅请求最近的 child_added 事件。我不确定如何使用您正在使用的 Python 库执行此操作,但在 JavaScript 中它将是 firebase.database().ref('accelerometerData/faultyMotorData').orderByKey().limitToLast(1).on('child_added'...
.
另请参阅:
关于python - 检索唯一的 child 从 firebase 添加到我在 firebase 中的监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53303695/