My first choice would be to request for all users from 20 to 25, and then filter the code for those 100.
The question is that filtering in the code is not an option, but I wanted to enable it for completeness in a situation 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)") } })
Note that we use .ChildAdded instead of .Value, so each node is read one at a time - if the distance is not what we want, we can ignore it and move on to the next.
source share