本文介绍了使用当前的时间戳在此之前查询所有帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我承诺一个简单的阅读:) 你好,我目前卡住了。我想查询从最新的帖子开始,而不是最旧的帖子的所有帖子。为什么?因为它是我可以在我的tableView中包含分页的唯一方法。 (这意味着当用户到达底部时重新加载) 可以说我有10个帖子,从最老的#1和最新的#10开始: 1,2 ,...,10 如果我查询限于前5后,我得到 [1,2,3,4,5] ,那么我倒过来的数组,将在我的桌面视图中以5开头显示 [5,4,3,2,1] 。 :问题是一旦用户到达tableview的底部,如果我查询下一个5个职位,从最后一个职位的时间戳开始,我得到... [6,7,8,9,10] 但现在我的表格视图不再正确了,因为它会显示 [5,4,3,2,1,10,9 ,8,7,6] 我希望,如果我可以使用当前的设备日期作为时间戳,并查询所有帖子之前如下所示:我基本上可以得到 [10, 9,8,7,6]然后当用户到达底部:[10,9,8,7,6,5,4,3,2,1] let todayTime:TimeInterval = NSDat e()。timeIntervalSince1970 FIRDatabase.database()。参考()。child(post)。child(animal) .queryOrderedByValue()。queryEnding(atValue:todayTime,childKey:datePost)。queryLimited(toLast:5).observe(.value,with:{(snapshot)in pre 解决方案鉴于以下结构 消息 msg_0 msg:最早的消息时间戳:-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 和下面的代码 let messagesRef = self.ref.child(messages) let queryRef = messagesRef.queryOrdered(byChild: timestamp) queryRef.observe(.childAdded,with:{snapshot in let key = snapshot.key 打印(键)}) 输出将会是 msg_3 msg_1 msg_4 msg_2 msg_0 正如您所看到的,msg_3是最新的消息,并显示在顶部,即它是第一个快照查询收到。如果你正在填充一个数组作为数据源,这将被添加到索引0,然后msg_1将在索引1等。 如果用户滚动和你想加载下一个5,它将是时间戳6到10,10是最新的。现在假设你想加载最旧的两个消息。这里是查询 let queryRef = messagesRef.queryOrdered(byChild:timestamp) .queryStarting(atValue: - 3,childKey:timestamp) .queryEnding(atValue:-1,childKey:timestamp) 结果 msg_2 msg_0 然后,我们要载入下一个最早的两个 let queryRef = messagesRef.queryOrdered(byChild:timestamp) .queryStarting(atValue:-5,childKey:timestamp) .queryEnding(atValue:-3,childKey:timestamp) 这给了我们 msg_1 msg_4 很显然,我用-5,-4等对于一个实际的时间戳,但它会以同样的方式。 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,...,10If 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 解决方案 Given the following structuremessages 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: -3and the following codelet 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 messagemsg_1msg_4msg_2msg_0 <-oldest messageAs 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 querylet queryRef = messagesRef.queryOrdered(byChild: "timestamp") .queryStarting(atValue: -3, childKey: "timestamp") .queryEnding(atValue: -1, childKey: "timestamp")the resultmsg_2msg_0then, we want to load in the next oldest twolet queryRef = messagesRef.queryOrdered(byChild: "timestamp") .queryStarting(atValue: -5, childKey: "timestamp") .queryEnding(atValue: -3, childKey: "timestamp")which gives usmsg_1msg_4Obviously I substitutes -5, -4 etc for an actual timestamp but it would work the same way. 这篇关于使用当前的时间戳在此之前查询所有帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 20:36