0

So, say each user in a RTDB has a child called caption. This child is a timestamp of seconds since 1970. Now you want to display all users who have a timestamp that is in today. You need to query the database for all users with a timestamp that is in today, because you don't want to send all users to the phone and then filter.

Below, in the first code is how I am currently sending all users to the device and then filtering. The second set of code is where I am attempting to first query all users who have a timestamp in today, ie before sending all to the users device.

One idea would be to make a firebase entry automatically each day that just represents the day. Then just compare that to all the timestamps of the users. The issue I have with that is that timezones are different.

var dict = CLLocation()
         ...
dict = CLLocation(latitude: lat, longitude: lon)
        ...
let thisUsersUid = Auth.auth().currentUser?.uid
self.refArtists2 = Database.database().reference().child("people");                                            self.refArtists2.observe(DataEventType.value,  with: {  snapshot in
if snapshot.childrenCount>0{
   self.people.removeAll()
            for people in snapshot.children.allObjects as! [DataSnapshot] {
                if people.key != thisUsersUid {
                let peopleObject = people.value as? [String: AnyObject]
                let peoplecaption = peopleObject?["caption"] as? Int //is timestamp
                let coordSnap12 = people.childSnapshot(forPath: "caption").value as? Int ?? 0
                let date = Date(timeIntervalSince1970: TimeInterval(coordSnap12)/1000.0)
                //let secondsInDay = 86400
                **if Calendar.current.isDateInToday(date)** {

.

 var ppp: String!  ////this should be the uid of all users in db

    let people = Database.database().reference().child("people").child(self.ppp).child("captions")
people.observe(DataEventType.value, with: {  snapshot in
    let captionss = snapshot.value as? Int ?? 0
    let date = Date(timeIntervalSince1970: TimeInterval(captionss)/1000.0)       
    let query1 = Database.database().reference().child("people").queryOrdered(byChild: "caption").where?(isDateInToday(date))
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
uuuuuu
  • 119
  • 1
  • 7
  • 2
    If the timestamp is an `Int` calculate the timestamp range for today (once) and get all records whose timestamp is higher than the lower bound and lower than the upper bound. This converting back and forth from `Date` to `Int` is inefficient. – vadian Nov 10 '20 at 12:08
  • thanks. "If the timestamp is an Int calculate the timestamp range for today " - how though? And lower than upper bound would not need to be checked, because it is Impossible for a user's timestamp to be higher than today – uuuuuu Nov 10 '20 at 13:18
  • 1
    For example the timestamp range for today, Nov 10th, 2020, in milliseconds since 1970 is `1604962800000..<1605049200000`. If `captionss` is in this range the date is in today. – vadian Nov 10 '20 at 14:36
  • Hey @vadian. That sounds like the start of a great answer, as `queryStarting(atValue:)` and `queryEnding(atValue:)` are indeed the key to accomplishing use-case. Can you write it down in an answer below? – Frank van Puffelen Nov 10 '20 at 14:37
  • @FrankvanPuffelen Sorry, I'm not familiar with Firebase. I just wanted to give the OP a starting point. – vadian Nov 10 '20 at 14:39
  • Double kudos in that case for getting the API correctly. :) I wrote up a simple answer+code sample below. – Frank van Puffelen Nov 10 '20 at 15:13

1 Answers1

0

As vadian commented, you'll want to perform a range query to get those nodes. In Firebase Realtime Database you do that with the queryStarting(atValue:) and queryEnding(atValue:) methods, so it'll be something like:

Database.database().reference().child("people")
    .queryOrdered(byChild: "caption")
    .queryStarting(atValue: 1604962800)
    .queryEnding(atValue: 1605049200)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks. That makes sense. However, the big issue is getting the `queryStarting(atValue`, which should be whenever today started in whatever timezone the user is. Do you know how that can be done? – uuuuuu Nov 10 '20 at 17:35
  • This seems promising: https://stackoverflow.com/a/54084211/209103 – Frank van Puffelen Nov 10 '20 at 21:24
  • Thanks, that was seconds since 1970. I *1000 to have it be comparable with the milliseconds since 1970 in the caption child. Then it works. – uuuuuu Nov 11 '20 at 11:03