我有这个Firebase数据:
我想通过分页查询帖子数据.目前我的代码正在将此JS代码转换为Swift代码
let postsRef = self.rootDatabaseReference.child("development/posts") postsRef.queryOrderedByChild("createdAt").queryStartingAtValue((page - 1) * count).queryLimitedToFirst(UInt(count)).observeSingleEventOfType(.Value,withBlock: { snapshot in .... })
访问时,此数据页:1,计数:1.我可以获取“posts.a”的数据但是当我尝试访问页面时:2,计数:1返回仍然是“posts.a”
我在这里想念的是什么?
假设您在将数据推送到Firebase时正在或将要使用childByAutoId(),您可以使用queryOrderedByKey()按时间顺序排序数据. Doc
here.
The unique key is based on a timestamp,so list items will automatically be ordered chronologically.
要启动特定键,您必须使用queryStartingAtValue(_ :)附加查询.
样品用法:
var count = numberOfItemsPerPage var query ref.queryOrderedByKey() if startKey != nil { query = query.queryStartingAtValue(startKey) count += 1 } query.queryLimitedToFirst(UInt(count)).observeSingleEventOfType(.Value,withBlock: { snapshot in guard var children = snapshot.children.allObjects as? [FIRDataSnapshot] else { // Handle error return } if startKey != nil && !children.isEmpty { children.removeFirst() } // Do something with children })
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。