大佬教程收集整理的这篇文章主要介绍了swift – Firebase查询,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
-users -user1_uid name distance age
我如何进行查询(查找距离<100且年龄介于20到25之间的用户)? 我试过标准方法
let recentPostsQuery = (ref?.child("users").queryorderedByChild("age").queryStarTingAtValue("20"))!
问题是,这似乎不可能查询多个孩子(如结合年龄和距离过滤).几个月前与Firebase相比,这方面有什么变化吗?我相信,在第一次查询后在本地过滤它们不是一种选择,因为可能存在数千个对象.
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!)") } }) }
现在一个潜在的超简单答案.
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)") } })
请注意,我们正在利用.ChildAdded而不是.Value,因此每次读取一个节点 – 如果距离不是我们想要的,我们可以忽略它并继续下一个节点.
以上是大佬教程为你收集整理的swift – Firebase查询全部内容,希望文章能够帮你解决swift – Firebase查询所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。