我正在尝试删除FirebaseDB中插入的GeoLocations,如下所示:
map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
@Override
public void onMapLongClick(final LatLng latLng) {
dbRef.child("hora").setValue(ServerValue.TIMESTAMP); //Executes TIMESTAMP function in firebase server and stores that value
dbRef.child("hora").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
hora = String.valueOf(dataSnapshot.getValue());
geoFire.setLocation(hora, new GeoLocation(latLng.latitude,latLng.longitude), new GeoFire.CompletionListener() {
@Override //Save geolocation with timestamp in seconds
public void onComplete(String key, DatabaseError error) {
if (error != null) {
Log.v("Informe","There was an error saving the location to GeoFire: " + error);
} else {
Log.v("Informe","Location saved on server successfully!");
}
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
在数据库中,这将如下所示:
{
"geofire" : {
"1515692377982" : {
".priority" : "u1dfw2vsqa",
"g" : "u2xfw2vsqe",
"l" : [ 48.25681948174579, 22.430931776762012 ]
},
"1515692378159" : {
".priority" : "u1dfw2vsqa",
"g" : "u2xfw2vsqe",
"l" : [ 48.25681948174579, 22.430931776762012 ]
}
},
"hora" : 1515692378159
}
(我不知道为什么只用一次长按就插入了两次)
我已经搜索了这个,我知道我必须使用云功能,但我必须知道JS,我不太了解它,所以,有人可能想帮我做一个删除数据的功能例如,在火堆中停留超过1小时?
我知道这必须由crontab和HTTPS触发,但我需要先检查该函数是否有效。 我发现一些有趣的链接:
Delete items that have been in the DB for 2 hours
Delete old users through crontab and HTTPS
我认为它应该是这两个链接的混合。
答案 0 :(得分:1)
最后,我得到了它的工作,所以在这里,如果有人需要谷歌云功能来删除超过一小时前的火力棒数据(这是由带有时间戳的键设置,请查看我的数据模型),现在你可以安排crontab每隔一段时间运行一次HTTPS请求。
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const ref = admin.database().ref().child('geofire'); //Path to the child that contains the items you want to delete
const CUT_OFF_TIME = 3600000; //1 Hour
exports.cleanOldFiles = functions.https.onRequest((req, res) => {
const now = Date.now();
const cutoff = now - CUT_OFF_TIME;
const lastHour = '' + cutoff; //Must be String in order to put it inside .orderByKey().endAt()
const query = ref.orderByKey().endAt(lastHour);
return query.once('value').then(snapshot => {
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(child => {
updates[child.key] = null;
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
});
});