SIRS
SIRS

Reputation: 624

Query firebase data with dynamically current timestampt

I have a data something like this:

+ item1
    |---- date : 1512008295000
    |---- etc : etc

I use this query to get data start at current timestamp

Query query = Utils.getFirebaseDb().getReference()
        .child("registration")
        .child(mUser.getUid())
        .orderByChild("date")
        .startAt(DateTime.now().getMillis());

however, the DateTime.now().getMillis() is always changing,

suppose I query this on 1512008080000 which is, November 30, 2017 2:14:40, I will got the item with date : 1512008295000 which is November 30, 2017 2:18:15, then 5 minute later, I still got item with date : 1512008295000, even current timestamp is 1512008380000 or November 30, 2017 2:19:40.

How i can query with dynamically changing current timestamp.

Upvotes: 1

Views: 225

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 599581

There is no way to pass a dynamic timestamp into a Firebase Database query. If you want to drop the items dynamically can either change the query regularly and/or remove the outdated items from the display locally.

Upvotes: 0

nyongrand
nyongrand

Reputation: 618

Maybe it's not answer to your question, but I think it's a workarround to your problem.

First, add new node that identity the item date is passed, something like this

+ item1
    |---- date : 1512008295000
    |---- passed : false
    |---- etc : etc

then change your query to this:

Query query = Utils.getFirebaseDb().getReference()
        .child("pendaftaran")
        .child(mUser.getUid())
        .orderByChild("passed")
        .equalTo(false);

Then on your onBindViewHolder check if the date is passed, and then change passed value to true if it already passed:

@Override
protected void onBindViewHolder(YourHolder holder, int position, Model model) {

    boolean passed = model.getDate() < DateTime.now().getMillis();
    if (passed) {
       getRef(position).child("passed").setValue(true);
    } 
}

or if you do not need the passed item anymore, just delete it

getRef(position).removeValue();

whit this, you do not need to create additional node, and you still can order the query using date.

Upvotes: 3

Related Questions