如何过滤Swift中的Firebase数据? [英] How to filter Firebase data in Swift?
问题描述
Title
,描述
和一个已发布
标志(请参阅下面的屏幕截图以进行说明)。
在应用程序中,我想过滤数据,只显示主题 published = true
。
这就是我想要做的:
$ $ $ $ $ $ $ $ $ self.ref = FIRDatabase.database ().referenceFromURL(FIREBASE_URL).child(topics)
self.ref?.queryEqualToValue(published)
self.ref?.observeEventType(.Value,withBlock:{(snapshot)in
//...处理快照
})
不工作。我应该如何处理这个?在此先感谢您的帮助。
您在这里有一些小错误。总的来说,没有什么不好的,但总的来说,他们永远不会工作:
- 调用任何
查询...
方法返回一个新对象
- 需要
orderByChild()
才能过滤其值 li>
- 您需要循环搜索结果
结合以下内容:
let ref = FIRDatabase.database()。referenceFromURL(FIREBASE_URL).child(topics)
let query = ref.queryOrderedByChild(published ).queryEqualToValue(true)
snapshot.children {
print(childSnapshot)
} $ b $ query.observeEventType(.Value,withBlock:{(snapshot)in
for childSnapshot)
$ b我们经常得到这个问题。例如,从昨天看起来非常相似: Firebase查询不正确执行。由于我的解释随着每个答案的不同,我建议浏览一下阅读我的相关答案,直到点击。
Basically, I have a structure called, topics which contains Title
, Description
and a Published
flag (see screenshot below for clarification ).
In the application, I want to filter the data and only show the topics that have published = true
.
This is what I'm trying to do:
self.ref = FIRDatabase.database().referenceFromURL(FIREBASE_URL).child("topics")
self.ref?.queryEqualToValue("published")
self.ref?.observeEventType(.Value, withBlock: { (snapshot) in
//...Handle Snapshot here
})
But this is not working. How should I approach this? Thanks in advance for the help.
解决方案 You have a few small mistakes in there. Overall nothing too bad, but combined they'll never work:
- calling any of the
query...
methods returns a new object
- you need to
orderByChild()
before you can filter on its value
- you need to loop over the results
Combining these:
let ref = FIRDatabase.database().referenceFromURL(FIREBASE_URL).child("topics")
let query = ref.queryOrderedByChild("published").queryEqualToValue(true)
query.observeEventType(.Value, withBlock: { (snapshot) in
for childSnapshot in snapshot.children {
print(childSnapshot)
}
})
We get this question regularly. For example, this from yesterday looks very similar: Firebase Query not Executing Properly. Since my explanation varies with every answer, I recommend browsing a bit to read my relevant answers until it clicks.
这篇关于如何过滤Swift中的Firebase数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!