24hrcoder
24hrcoder

Reputation: 15

Using the current timestamp to query all post before that time

I promise an EASY read :)

Howdy, I am currently stuck. I will like to query all post starting with the newest post first instead of the oldest post. Why? Because its the only way I can include pagination in my tableView. (which means reloading when user gets to the bottom)

Lets say I have 10 posts starting with the oldest #1 and the newest #10: 1,2,...,10 If I query limited to the first 5 post I get [1,2,3,4,5] then I reverse the array, which will show [5,4,3,2,1] on my tableview starting with 5.

ISSUE: The problem is once the user gets to the bottom of the tableview, if i query the next five posts starting with the timestamp of the last post, I get... [6,7,8,9,10]

But now my table view isn't correct anymore as it will show [5,4,3,2,1,10,9,8,7,6] I am hoping that if I can use the current device date as a timestamp and query the all post before as shown below: I can essentially get [10,9,8,7,6] then when user reaches the bottom: [10,9,8,7,6,5,4,3,2,1]

let todayTime: TimeInterval = NSDate().timeIntervalSince1970

          FIRDatabase.database().reference().child("post").child(animal).queryOrderedByValue().queryEnding(atValue: todayTime, childKey: "datePost").queryLimited(toLast: 5).observe(.value, with: {(snapshot) in

Upvotes: 0

Views: 448

Answers (1)

Jay
Jay

Reputation: 35658

Given the following structure

messages
  msg_0
    msg: "oldest message"
    timestamp: -1
  msg_1
    msg: "hello"
    timestamp: -4
  msg_2
    msg: "hello"
    timestamp: -2
  msg_3
    msg: "newest message"
    timestamp: -5
  msg_4
    msg: "hello"
    timestamp: -3

and the following code

let messagesRef = self.ref.child("messages")
let queryRef = messagesRef.queryOrdered(byChild: "timestamp")
queryRef.observe(.childAdded, with: { snapshot in
    let key = snapshot.key
    print(key)
})

the output will be

msg_3 <-newest message
msg_1
msg_4
msg_2
msg_0 <-oldest message

As you can see, msg_3 is the newest message and appears 'at the top' i.e. it's the first snapshot the query receives. If you are populating an array to be used as a datasource, this will be added at index 0, then msg_1 would be at index 1 etc.

If the user scrolls and you want to load the next 5, it would be timestamps 6 to 10, with 10 being the newest.

Now suppose you want to load in the oldest two messages. Here's the query

let queryRef = messagesRef.queryOrdered(byChild: "timestamp")
                          .queryStarting(atValue: -3, childKey: "timestamp")
                          .queryEnding(atValue: -1, childKey: "timestamp")

the result

msg_2
msg_0

then, we want to load in the next oldest two

let queryRef = messagesRef.queryOrdered(byChild: "timestamp")
                          .queryStarting(atValue: -5, childKey: "timestamp")
                          .queryEnding(atValue: -3, childKey: "timestamp")

which gives us

msg_1
msg_4

Obviously I substitutes -5, -4 etc for an actual timestamp but it would work the same way.

Upvotes: 1

Related Questions