如何过滤Swift中的Firebase数据? [英] How to filter Firebase data in Swift?

查看:133
本文介绍了如何过滤Swift中的Firebase数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个称为主题的结构,它包含 Title 描述和一个已发布标志(请参阅下面的屏幕截图以进行说明)。



在应用程序中,我想过滤数据,只显示主题 published = true



这就是我想要做的:

$ $ $ $ $ $ $ $ $ self.ref = FIRDatabase.database ().referenceFromURL(FIREBASE_URL).child(topics)
self.ref?.queryEqualToValue(published)
self.ref?.observeEventType(.Value,withBlock:{(snapshot)in
//...处理快照
})

不工作。我应该如何处理这个?在此先感谢您的帮助。

解决方案

您在这里有一些小错误。总的来说,没有什么不好的,但总的来说,他们永远不会工作:


  1. 调用任何查询... 方法返回一个新对象
  2. 需要 orderByChild()才能过滤其值 li>
  3. 您需要循环搜索结果

结合以下内容:

  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:

  1. calling any of the query... methods returns a new object
  2. you need to orderByChild() before you can filter on its value
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆