Firebase Swift:childKey的queryEqualToValue不起作用
有没有人有任何运气使用的function:
.queryEqualToValue(value: AnyObject?, childKey: String?)
两件事情:
1)childKey似乎不允许深度path,只是直接的孩子
2)我根本无法工作! 鉴于我的结构:
"post": { "groupName": "hello world" }
如果我做一个简单的:
postRef.queryEqualToValue("hello world", childKey: "groupName").observeSingleEvent( ... )
它根本不返回任何post。 相反,我必须做的迂回的方式:
postRef.queryOrderedByChild("groupName").queryEqualToValue("hello world").observeSingleEvent( ... )
得到它的工作!
我是否错误地使用上述function?
Xcode 7.2.1
Swift 2.1.1
iOS 9.2
OSX 10.10.5
2)我根本无法工作! 鉴于我的结构:
"post": { "groupName": "hello world" }
如果我做一个简单的:
postRef.queryEqualToValue("hello world", childKey: "groupName") postRef.queryEqualToValue("hello world", childKey: "groupName").observeSingleEvent( ... )
我可以用下面的代码成功获取查询:
import UIKit import Firebase class ViewController: UIViewController { var dbRef: FIRDatabaseReference! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. dbRef = FIRDatabase.database().reference() dbRef.child("post").child("groupName").setValue("hello world") let postRef = dbRef.child("post") print(postRef) let query = postRef.queryEqualToValue("hello world", childKey: "groupName") print(query) --output:-- (/post { en = groupName; ep = "hello world"; sn = groupName; sp = "hello world"; })
1)childKey似乎不允许深度path,只是直接的孩子
我没有看到。 如果我将我的代码更改为:
import UIKit import Firebase class ViewController: UIViewController { var dbRef: FIRDatabaseReference! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. dbRef = FIRDatabase.database().reference() dbRef.child("post").child("groupName").child("userId").setValue("hello world") let postRef = dbRef.child("post") print(postRef) let query = postRef.queryEqualToValue("hello world", childKey: "userId") print(query)
我得到以下输出:
(/post { en = userId; ep = "hello world"; sn = userId; sp = "hello world"; })
但是,我不能让观察员工作 – 他们肯定不像文档所说的那样工作。
我在运行应用程序之前删除了Firebase上的所有数据。
编辑 :好吧,我想我知道我的观察员正在发生什么事情。 根据检索数据部分:
通过将asynchronous侦听器附加到
FIRDatabase引用FIRDatabaseReference来检索Firebase数据。 对于数据的初始状态,监听器被触发一次,并且在任何时候数据改变。
当我尝试观察查询时,callback中的快照没有任何数据。 另一方面,当我观察FirDatabaseReference
,观察者(有点)像我所期望的那样工作。
例如,如果我观察这样一个FIRDatabaseReference:
import UIKit import Firebase class ViewController: UIViewController { var dbRef: FIRDatabaseReference! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. dbRef = FIRDatabase.database().reference() dbRef.child("post").child("groupName").child("userId").setValue("hello world") let postRef = dbRef.child("post") postRef.observeEventType(.Value, withBlock: { (snapshot) -> Void in print(snapshot) }) sleep(1) //Allow the observer (in another thread) to execute before the following line: dbRef.child("post").child("groupName").child("userId").setValue("goodbye Mars") }
那么在callback中的快照中有数据,我得到这个输出:
Snap (post) { groupName = { userId = "hello world"; }; } Snap (post) { groupName = { userId = "goodbye Mars"; }; }
但是,如果我观察一个FIRDatabaseQuery:
let postRef = dbRef.child("post") let query = postRef.queryEqualToValue("hello world", childKey: "userID") query.observeEventType(.Value, withBlock: { (snapshot) -> Void in print(snapshot) })
那么输出结果是:
Snap (post) <null>
以下内容是在Firebase文档中编写的:
queryEqualToValue返回与指定的键,值或优先级相等的项目,具体取决于所选的sorting方式。
在iOS上检索数据
重要的部分是: “依靠select顺序的方法”。
因此,请确保将queryEqualToValue -Method与orderBy -Method结合使用 。
例如:
let groupState = "open" ref.child("groups").queryOrdered(byChild:"state").queryEqual(toValue:groupState).observe(enventType: .value, with: { snapshot in // Returns all groups with state "open" for group in snapshot.children { print(group) } })
PS:带有childKey的变体在新的Firebase iOS-指南中没有提及。