schwdim
schwdim

Reputation: 193

Firebase Query filtered by creation time and where date is greater than now

I'm kinda new to Firebase and I'm trying to create an app with it.

My app has posts that are created at a certain time and they contain a certain time.

I store my data the following way :

"posts" : {
    "postid1" : {
      "author" : {
        "full_name" : "author1",
        "uid" : "idauthor1"
      },
      "date" : 1474455840809,
      "description" : "",
      "title" : "Test 1"
    },
    "postid2" : {
      "author" : {
        "full_name" : "author1",
        "uid" : "idauthor1"
      },
      "date" : 1480424400622,
      "description" : "",
      "title" : "Test 2"
    }
}

The postID is generated automatically so that should be enough to order them by creation time, but I also want to exclude the ones where the date is past today (now).

Does anyone have an idea how I could/should do that?

Upvotes: 0

Views: 13374

Answers (2)

Wilik
Wilik

Reputation: 7720

The query for that should be

// server_timestamp is a long type variable
ref.child("posts").orderByChild("date").startAt(server_timestamp);

The problem is (as mentioned by @Bonja) you can't pass Firebase's constant ServerValue.TIMESTAMPto that method.

So, you can either do this (not recommended because the time is based on user's device timezone and it might not synced correctly)

Long server_timestamp = new Date().getTime();

or my own hacky way (saving ServerValue.TIMESTAMP to a special node and then get the value. and yes, it's a lot of work to do)

ref.child("current_timestamp").setValue(ServerValue.TIMESTAMP, new DatabaseReference.CompletionListener() {
    @Override
    public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Long server_timestamp = dataSnapshot.getValue(Long.class);
                ref.child("posts").orderByChild("date").startAt(server_timestamp).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        Post post = dataSnapshot.getValue(Post.class);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {}
                });
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });
    }
});

Hope this helps :)

Upvotes: 3

Borja
Borja

Reputation: 1289

I suppose you are saving date with "ServerValue.Timestamp".

I think what you want to achieve is not possible directly because you should pass a date (today) as an argument, and you can't put ServerValue.Timestamp.

What I would do is to query all the posts this way: ref.orderByChild("date")

And then filtering in client-side.

EDIT This should work. (it returns posts with date after today, local time)

query.orderByChild("date").startAt(new DateTime().getMillis())

Then you should order by key in client-side

Upvotes: 9

Related Questions