问题描述
我在 FirebaseDatabase
中有一些数据,其中每个数据集都有两个属性: startTime
和 endTime
。
I have some data in FirebaseDatabase
in which every data set has two properties: startTime
and endTime
.
以下是数据结构:
app
-ref
-uniqueID1
-key: value
-startTime: 1488849333
-endTime: 1488853842
-key: value
-uniqueID2
-key: value
-startTime: 1488850198
-endTime: 1488853802
-key: value
我想要的是在 endTime
自动过去或用户打开应用程序时删除数据集。
What I want is to deleted the data set when the endTime
has passed automatically or when the user opens the app.
我已经对此主题进行了一些研究,发现 ,但这对我似乎没有帮助。
I have done some research on this topic and found this, but this doesn't seems helpful to me.
我如何删除其 endTime
已通过?
推荐答案
这看起来像是一个类似的答案,但您似乎不明白答案。所以这是怎么做。
因为重要的是结束时间,所以我们只会监视结束时间。为此,我们获取当前时间
This looks like a similar answere but you seem not to understand the answer. So here is how to do it.Since what matters is the end time only we are going to monitor when it has passed. To do this, we get the current time
Date().getTime();
并检查其是否大于结束时间(如果大于,则表示结束时间
and check whether its greater than the end time(if it is, it means that the end time has passed)
final DatabaseReference currentRef = adapter.getRef(position);
currentRef.child("endTime").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
long time = System.currentTimeMillis(); //get time in millis
long end = Long.parseLong( dataSnapshot.getValue().toString()); //get the end time from firebase database
//convert to int
int timenow = (int) time;
int endtime = (int) end;
//check if the endtime has been reached
if (end < time){
currentRef.removeValue(); //remove the entry
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
当我要删除的项目被点击时,我已经实现了该代码。因此适配器来自列表视图。
谢谢,希望对您有帮助
i have implemented that code when the item i want to remove is clicked. so the adapter is from a listview.Thanks, i hope it helps
这篇关于经过指定时间后,将自动删除一组Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!