0

I am developing kind of event organising app. So, I am saving the startTime, startdate, endTime and endDate of the event in FirebaseDatabase in following format: hh:mm:ss and dd-MM-yyyy respectively.

I'm saving under this reference:

mDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl("https://example-113be.firebaseio.com/events/")
mDatabase.child(itemID).setValue(events);

where itemID is push key retrieved using this code: itemID = mDatabase.push().getKey();

The problem: So, what I'm trying to do is I'm trying to filter the data based on the startTime and startDate in 2 categories - "Live events" and "events scheduled for later". Like if startTime of event is less than current time of device, it means the event has already started and should be showed under "live event" category and if the startTime is greater than current time, it means the event hasn"t been started yet and should be shown under "scheduled for later" category.

What I want: is to retrieve the data is such a manner that above described problem should be solved. Please let me know how to.

Please tell me if I need to restructure the way I'm saving data and should I need to store something else along with all the data.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Hammad Nasir
  • 2,889
  • 7
  • 52
  • 133

2 Answers2

2

Don't save time and date in string format. Use UNIX-timestamp format, so you can easily filter and sort in any way you want

But don't forget to specify .indexOn in database rules

Example of retrieving all events, that will happen next day

final long now = System.currentTimeMillis();
mDatabase.orderByChild("endTimestamp")
    .startAt(now).endAt(now + DateUtils.DAY_IN_MILLIS)
    .addChildEventListener(...);

.indexOn is helper for Firebase, so it will know, how index you data.

{
  "rules": {
    "$eventKey": {
      ".read": "true",
      ".write": "true",
      ".indexOn": "["endTimestamp","startTimestamp"]"
    }
  }
}
Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86
1

I recommend you to save the date and time in Long epoch format. Epoch explanation in wikipedia. For example : 1471446000000 in human time is 17 Aug 2016 15:00:00 (look at this site to understand how the conversion works)

With this, you just need startDateTime and endDateTime in the database structure. And then you can do query.startAt(currentDeviceTimeInEpoch, "startDateTime"); to get future events.

For "live events", the solution that I can think of is to retrieve all the events first and then you can do basic selection if (currentTime > startDateTime && currentTime < endDateTime)

Wilik
  • 7,630
  • 3
  • 29
  • 35
  • but how to query the database using `if (currentTime > startDateTime && currentTime < endDateTime)`? – Hammad Nasir Nov 28 '16 at 15:15
  • 1
    that's the logic inside the `onDataChanged` inside the `ValueEventListener`. To get the database, just attach the listener as usual `ref.addValueEventListener(valueEventListener);` @HammadNasir – Wilik Nov 28 '16 at 15:26