Firebase查询 [英] Firebase querying

查看:147
本文介绍了Firebase查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  -users 
-user1_uid
name
距离
年龄

如何查询<100和20至25岁之间)?

我已经尝试了标准方法

  let recentPostsQuery =(ref?.child(users)。queryOrderedByChild(age)。queryStartingAtValue(20))! 

M问题是,看起来不可能查询多个孩子距离过滤)。与几个月前的Firebase相比,在这方面做了些什么改变?我相信在第一次查询之后在本地过滤它们是不可能的,因为可能有数以千计的对象。

解决方案

第一选择将是查询20到25之间的所有用户,然后在代码中过滤距离< 100
$ b

这个问题表明,在代码中进行过滤不是一种选择,但是我想把它包括在数千个节点或更少的情况下:

  struct User {//开始一个结构来保存用户数据
var firebaseKey:String?
var theAge:Int?
var theDistance:Int?

$ b $ var userArray = [User]()//用户结构数组
$ b $ usersRef.queryOrderedByChild(age)。queryStartingAtValue(20)
.queryEndingAtValue(25).observeEventType(.Value,withBlock:{snapshot in

for snapshot.children中的子元素{//Value遍历节点

让age = child.value [age] as!Int
let distance = child.value [distance] as!Int
let fbKey = child.key!

让u = User(firebaseKey:fbKey,theAge:age,theDistance:distance)

userArray.append(u)//将用户结构添加到数组
}

//包含过滤用户的数组
var filteredArray:[User] = []
filteredArray = userArray.filter({$ 0.theDistance< 100})//过滤它,宝贝!

//将结果用户打印出来作为测试。
for filteredArray中的用户{

le tk = aUser.firebaseKey
let a = aUser.theAge
let d = aUser.theDistance

print(array:\(k!)\(a!) \(d!))

}

})
}



现在有一个超级简单的答案。
$ b $ pre $ let usersRef = self.myRootRef.childByAppendingPath (users)

usersRef.queryOrderedByChild(age)。queryStartingAtValue(20)
.queryEndingAtValue(25).observeEventType(.ChildAdded,withBlock:{snapshot in

let distance = snapshot.value [distance] as! Int

如果距离< 100 {
let age = snapshot.value [age] as! Int
让fbKey = snapshot.key!
$ b print(array:\(fbKey)\(age)\(distance))
}
})

Let's say I have a structure like this:

-users
  -user1_uid
    name
    distance
    age

How would I do a query like (Find users with distance <100 and age between 20 and 25)?

I have tried the standard method

        let recentPostsQuery = (ref?.child("users").queryOrderedByChild("age").queryStartingAtValue("20"))!

M problem is, that is does not appear to be possible to query multiple childs (like combining age and distance filtering). Did something change in this regard compared to Firebase a few months ago? Filtering them locally after the first query is not an option, I believe, as there could be potentially thousands of objects.

解决方案

My first choice would be to query for all the users between 20 and 25 and then filter in code for ones with a distance < 100.

The question states filtering in code is not an option but I wanted to include it for completeness for situations where it was several thousand nodes or less:

    struct User { //starting with a structure to hold user data
        var firebaseKey : String?
        var theAge: Int?
        var theDistance: Int?
    }

    var userArray = [User]() //the array of user structures

    usersRef.queryOrderedByChild("age").queryStartingAtValue(20)
     .queryEndingAtValue(25).observeEventType(.Value, withBlock: { snapshot in

        for child in snapshot.children { //.Value so iterate over nodes

            let age = child.value["age"] as! Int
            let distance = child.value["distance"] as! Int
            let fbKey = child.key!

            let u = User(firebaseKey: fbKey, theAge: age, theDistance: distance)

            userArray.append(u) //add the user struct to the array
        }

        //the array to contain the filtered users
        var filteredArray: [User] = []
        filteredArray = userArray.filter({$0.theDistance < 100}) //Filter it, baby!

        //print out the resulting users as a test.
        for aUser in filteredArray {

            let k = aUser.firebaseKey
            let a = aUser.theAge
            let d = aUser.theDistance

            print("array: \(k!)  \(a!)  \(d!)")

        }

    })
}

Now a potential super simple answer.

    let usersRef = self.myRootRef.childByAppendingPath("users")

    usersRef.queryOrderedByChild("age").queryStartingAtValue(20)
     .queryEndingAtValue(25).observeEventType(.ChildAdded, withBlock: { snapshot in

        let distance = snapshot.value["distance"] as! Int

        if distance < 100 {
            let age = snapshot.value["age"] as! Int
            let fbKey = snapshot.key!

            print("array: \(fbKey)  \(age)  \(distance)")
        }
    })

Notice that we are leveraging .ChildAdded instead of .Value so each node is read in one at a time - if the distance is not what we want, we can ignore it and move on to the next.

这篇关于Firebase查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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