如何阅读firebase数据库 [英] how to read firebase database
问题描述
现在我想在表格视图中加载一些俱乐部信息。当我向下滑动iPhone屏幕时,它会加载下一个俱乐部信息。
这是我的代码。但它加载所有俱乐部信息。
func loadClubs(){$>如何加载只有几个俱乐部,并加载下一个俱乐部时用户刷卡? b
/ pre>
$ b ref = Database.database()。reference()
ref.child(club)。observe(DataEventType.value,with:{(snapshot)in
$打印(俱乐部:\(快照))
让数组:NSArray = snapshot.children.allObjects作为NSArray
为数组obj {b $ b让快照:DataSnapshot = obj as!DataSnapshot
如果让childSnapshot = snapshot.value as?[String:AnyObject] {
如果让clubName = childSnapshot [name] as?String {
print(clubName)
$)
$ Firebase的查询支持分页,但与您以前的版本稍有不同。取而代之的是使用偏移量,Firebase使用所谓的锚点值来确定从何处开始。
获取第一页的内容很简单,只需指定一个限制
$ $ $ $ c $ ref = Database.database()。reference()
query = ref。 child(club)。queryOrderedByKey()。limitToFirst(10)
query.observe(DataEventType.value,其中:{(snapshot)in
现在在该块中,跟踪您向用户展示的最后一个项目的关键字:
对于数组obj {
let snapshot:DataSnapshot = obj as!DataSnapshot
如果让childSnapshot = snapshot.value as?[String:AnyObject] {
lastKey = childSnapshot.key
如果让clubName = childSnapshot [name] as?String {$ b $ print(clubName)
}
}
}
然后下一步页面,构建一个查询,从你看到的最后一个键开始:
query = ref.child(club) .queryOrderedByKey()。startAt(lastKey).limitToFirst(11)
您需要检索一个更多的项目比你的页面大小,因为在两个页面检索锚项目。
This is my data structure: many clubs, each club has address. I tried to make the database flat.
Now I want to load a few club info on table view. When I swipe down iPhone screen, it will load next a few club info.
This is my code. But it loads all club info. How can I load only a few club, and load next a few club when user swipe down?
func loadClubs() { ref = Database.database().reference() ref.child("club").observe(DataEventType.value, with: { (snapshot) in //print("clubs: \(snapshot)") let array:NSArray = snapshot.children.allObjects as NSArray for obj in array { let snapshot:DataSnapshot = obj as! DataSnapshot if let childSnapshot = snapshot.value as? [String: AnyObject] { if let clubName = childSnapshot["name"] as? String { print(clubName) } } } }) }
解决方案Firebase's queries support pagination, but it's slightly different from what you're used to. Instead of working with offsets, Firebase uses so-called anchor values to determine where to start.
Getting the items for the first page is easy, you just specify a limit to the number of items to retrieve:
ref = Database.database().reference() query = ref.child("club").queryOrderedByKey().limitToFirst(10) query.observe(DataEventType.value, with: { (snapshot) in
Now within the block, keep track of the key of the last item you've shown to the user:
for obj in array { let snapshot:DataSnapshot = obj as! DataSnapshot if let childSnapshot = snapshot.value as? [String: AnyObject] { lastKey = childSnapshot.key if let clubName = childSnapshot["name"] as? String { print(clubName) } } }
Then to get the next page, construct a query that starts at the last key you've seen:
query = ref.child("club").queryOrderedByKey().startAt(lastKey).limitToFirst(11)
You'll need to retrieve one more item than your page size, since the anchor item is retrieve in both pages.
这篇关于如何阅读firebase数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!