【问题标题】:how to read firebase database如何读取firebase数据库
【发布时间】:2017-08-28 08:02:31
【问题描述】:

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

现在我想在桌面视图上加载一些俱乐部信息。当我向下滑动 iPhone 屏幕时,它会加载接下来的一些俱乐部信息。

这是我的代码。但它会加载所有俱乐部信息。如何在用户向下滑动时只加载几个俱乐部,并加载下几个俱乐部?

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)
                    }
                }
            }
        })

    }

【问题讨论】:

    标签: swift firebase-realtime-database


    【解决方案1】:

    Firebase 的查询支持分页,但与您习惯的略有不同。 Firebase 不使用偏移量,而是使用所谓的锚值来确定从哪里开始。

    获取第一页的项目很容易,您只需指定要检索的项目数量限制:

        ref = Database.database().reference()
        query = ref.child("club").queryOrderedByKey().limitToFirst(10)
    
        query.observe(DataEventType.value, with: { (snapshot) in
    

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

       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)
                }
            }
        }
    

    然后要获取下一页,请构造一个从您看到的最后一个键开始的查询:

        query = ref.child("club").queryOrderedByKey().startAt(lastKey).limitToFirst(11)
    

    您需要比页面大小多检索一项,因为在两个页面中都会检索锚项。

    【讨论】:

      【解决方案2】:

      我认为正确的方法是引用元素数组, 并为索引制作变量

      var i = 0;
      var club = null;
      club = loadClubs(index); // here should return club with specified index;
                               // and increment index in loadClubs func,
                               // also if you need steped back --- and one more 
                               // argument to function, for example
                               // loadClubs(index, forward) // where forward is 
                               // boolean that says in which way we should 
                               // increment or decrement our index
      

      所以在你的例子中会是这样的:

      func loadClubs(index, forward) {
      
          ref = Database.database().reference()
      
          ref.child("club").observe(data => {
              var club = data[index];
              if(forward){
              index ++
              }else{
              index--
              }
          return club;
          })    
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-18
        相关资源
        最近更新 更多