I'm doing an app with a Firebase Realtime Database connection, the issue is that I'll use Arduino to send measured information to firebase and visualize this in the app and I don't want to maintain all the information in the DB, just for example the last 10 sensed values. The DB is structured this way,
How can achive that? i dont want to do it in the app, i would like to do it in firebase itself (cause the users of the app are not going to be all the time in the app, and arduino is gonna still sending info)
I hope that you can help me, thanks in advance!
CodePudding user response:
Instead of creating your schema like
Humedad
time_stamp_1: value
time_stamp_2: value
Do it something like,
Humedad
static_id_1
timestamp: value
reading: value
static_id_2
timestamp: value
reading: value
While updating / inserting these readings you loop for ten times on to rewrite,
["static_id_1", "static_id_2", "static_id_3", ..."static_id_10"]
these values and reset the loop in your arduino again to rewrite on same keys.
Please do mention which library you are using to communicate with firebase for detailed explanation.
CodePudding user response:
I don't want to do it in the app.
If you always need to have only 10 children available under a certain node, there is no need to do that in your application code. The simplest solution would be to write a function in Cloud Functions for Firebase, and each time the 11th element is written under that node, delete the oldest one. In this way, you'll only have 10 children.
If you intend in the future to create some statistics, then you should consider leaving the data as it is, and only retrieve the latest 10. Here's the official documentation regarding filtering data. So Query#limitToLast(int limit) will do the trick.