如何阅读firebase数据库 [英] how to read firebase database

查看:116
本文介绍了如何阅读firebase数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据结构:很多俱乐部,每个俱乐部都有地址。我试图使数据库持平。



现在我想在表格视图中加载一些俱乐部信息。当我向下滑动iPhone屏幕时,它会加载下一个俱乐部信息。





这是我的代码。但它加载所有俱乐部信息。

  func loadClubs(){$>如何加载只有几个俱乐部,并加载下一个俱乐部时用户刷卡? b 
$ 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
/ pre>

现在在该块中,跟踪您向用户展示的最后一个项目的关键字:

 对于数组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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆