Firebase Swift:childKey的queryEqualToValue不起作用 [英] Firebase Swift: queryEqualToValue by childKey is not working
问题描述
.queryEqualToValue(value:AnyObject?,childKey:String?)
两件事情:
$ b $ 1)childKey似乎不允许深度路径,只是直接的孩子
2)我根本无法得到它的工作!给定我的结构:
post:{
groupName:hello world
}
如果我做一个简单的话:
postRef.queryEqualToValue(hello world,childKey:groupName)。observeSingleEvent(...)
根本不返回任何帖子。相反,我必须做的迂回的方式:
postRef.queryOrderedByChild(groupName)。queryEqualToValue(hello world) .observeSingleEvent(...)
可以使用它!
我是否正确使用上述函数? 解码方案
1
Swift 2.1.1
iOS 9.2
OSX 10.10.5
$ b
2)我根本无法工作!给定我的结构:
post:{
groupName:hello world
}
如果我做一个简单的话:
<$ p (...)$ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
我可以通过以下代码成功获取查询: p>
导入UIKit
导入Firebase
类ViewController:UIViewController {
var dbRef:FIRDatabaseReference!
覆盖func viewDidLoad(){
super.viewDidLoad()
//加载视图之后,通常从一个笔尖执行任何额外的设置。
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)
- 输出: -
(/ post {
en = groupName;
ep =hello world ;
sn = groupName;
sp =hello world;
})
1)childKey似乎不允许深度路径,只是直接
children
我没有看到。如果我将代码更改为:
$ b $ pre $ 导入UIKit
导入Firebase
类ViewController: UIViewController {
var dbRef:FIRDatabaseReference!
覆盖func viewDidLoad(){
super.viewDidLoad()
//加载视图之后,通常从一个笔尖执行任何额外的设置。
$ b $ dbRef = FIRDatabase.database()。reference()
dbRef.child(post)。child(groupName)。child(userId)。setValue(hello世界)
let postRef = dbRef.child(post)
print(postRef)
let query = postRef.queryEqualToValue(hello world, childKey:userId)
print(query)
我得到以下输出: / p>
(/ post {
en = userId;
ep =hello world;
sn = userId;
sp =hello world;
})
但是,我不能让观察员工作 - 他们肯定不像文档所说的那样工作。
我在运行应用之前删除了Firebase上的所有数据。 $ b 编辑:好吧,我想我知道我的观察者正在发生什么。根据检索数据部分:
通过将异步侦听器附加到
FIRDatabase引用FIRDatabaseReference中,可以检索Firebase数据。监听器在数据的初始
状态被触发一次,并且在数据发生变化时再次触发。
当我尝试观察一个查询,回调中的快照没有任何数据。另一方面,当我观察 FirDatabaseReference
时,观察者的工作(有点)像我所期望的那样。
例如,如果我观察这样的FIRDatabaseReference:导入UIKit
导入Firebase
类ViewController:UIViewController {
var dbRef:FIRDatabaseReference!
覆盖func viewDidLoad(){
super.viewDidLoad()
//加载视图之后,通常从一个笔尖执行任何额外的设置。
$ b $ dbRef = FIRDatabase.database()。reference()
dbRef.child(post)。child(groupName)。child(userId)。setValue(hello世界)
let postRef = dbRef.child(post)
postRef.observeEventType(.Value,withBlock:{(snapshot) - >无效
print快照)
))
sleep(1)//允许观察者(在另一个线程中)在下列行之前执行:
dbRef.child(post)。 child(groupName)。child(userId)。setValue(goodbye Mars)
$ b}
$ p $那么回调中的快照就有数据了,我得到这个输出:
Snap(post){
groupName = {
userId =hello world;
};
snap(post){
groupName = {
userId =goodbye Mars;
};
}
但是,如果我观察FIRDatabaseQuery:
let postRef = dbRef.child(post)
/ pre>
let query = postRef.queryEqualToValue(hello world,childKey:userID)
query.observeEventType(.Value,withBlock:{(snapshot) - >
print(snapshot)
})
然后输出:
$ b $ pre $<空>
Has anyone have any luck using the function:
.queryEqualToValue(value: AnyObject?, childKey: String?)
Two things:
1) The childKey does not seem to allow deep paths, just direct children
2) I can't get it to work at all! Given my structure:
"post": {
"groupName": "hello world"
}
If I do a simple:
postRef.queryEqualToValue("hello world", childKey: "groupName").observeSingleEvent( ... )
It does not return any posts at all. Instead I have to do the roundabout way of:
postRef.queryOrderedByChild("groupName").queryEqualToValue("hello world").observeSingleEvent( ... )
to get it to work!
Am I using the above function incorrectly?
Xcode 7.2.1
Swift 2.1.1
iOS 9.2
OSX 10.10.5
2) I can't get it to work at all! Given my structure:
"post": { "groupName": "hello world" }
If I do a simple:
postRef.queryEqualToValue("hello world", childKey: "groupName") postRef.queryEqualToValue("hello world", childKey: "groupName").observeSingleEvent( ... )
I can successfully get the query with the following code:
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) The childKey does not seem to allow deep paths, just direct children
I'm not seeing that. If I change my code to:
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)
I get the following output:
(/post {
en = userId;
ep = "hello world";
sn = userId;
sp = "hello world";
})
However, I cannot get the observers to work--they certainly don't work like the docs say they do.
I delete all my data on Firebase before I run the app.
Edit: Okay, I think I know what's going on with my observers. According to the Retrieve Data section:
Firebase data is retrieved by attaching an asynchronous listener to a
FIRDatabase referenceFIRDatabaseReference. The listener is triggered once for the initial state of the data and again anytime the data changes.
When I try observing a query, the snapshot in the callback doesn't have any data in it. On the other hand, when I observe a FirDatabaseReference
the observer works (somewhat) like I expect.
For example, if I observe a FIRDatabaseReference like this:
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")
}
then the snapshot in the callback has data in it, and I get this output:
Snap (post) {
groupName = {
userId = "hello world";
};
}
Snap (post) {
groupName = {
userId = "goodbye Mars";
};
}
But if I observe a FIRDatabaseQuery:
let postRef = dbRef.child("post")
let query = postRef.queryEqualToValue("hello world", childKey: "userID")
query.observeEventType(.Value, withBlock: { (snapshot) -> Void in
print(snapshot)
})
then the output is:
Snap (post) <null>
这篇关于Firebase Swift:childKey的queryEqualToValue不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!