【问题标题】:Retrieving data from firebase depending on (City)根据(城市)从firebase检索数据
【发布时间】:2016-04-08 20:07:07
【问题描述】:

我正在使用 swift 开发 iOS 应用程序,并且我在应用程序的 firebase 仪表板中有以下数据

Users =     
{
    "07a5fa11-2a09-455b-92bf-a86dcd9d3e3e" =         
     {
        Name = "Hissah";
        Category = "Art & Designe";
        City = "Riyadh";
        Email = "H@him.fm";
        ShortDescription = "";
    };
    "08e5443c-cdde-4fda-8733-8c4fce75dd34" =         
    {
        Name = "Sara";
        Category = "Cheefs";
        City = "Dubai";
        Email = "Sara@gmail.com";
        ShortDescription = "best cake ever . ";
    };

如何将 (City) 为“Riyadh”的用户的 (Name) 检索到表格视图中?

提前致谢。

【问题讨论】:

  • Firebase 文档是一个很好的信息来源,可为许多常见用例提供解决方案。 documentation on querying 中涵盖了这个特定的

标签: ios swift firebase tableview


【解决方案1】:

把它扔进戒指,因为它是一个简单的答案,并解决了一个可用于填充 tableView 的数据源

let ref = Firebase(url:"https://your-app.firebaseio.com/users")

ref.queryOrderedByChild("City").queryEqualToValue("Riyadh")
   .observeEventType(.Value, withBlock: { snapshot in

            //iterate over all the values read in and add each name
            //  to an array
            for child in snapshot.children {
              let name = child.value["Name"] as! NSString
              self.tableViewDataSourceArray.append(name)
            }

            //the tableView uses the tableViewDataSourceArray
            // as it's dataSource
            self.tableView.reloadData() 

})

编辑:后续评论询问如何将文本添加到 NSTextView

ref.queryOrderedByChild("City").queryEqualToValue("Riyadh")
   .observeEventType(.Value, withBlock: { snapshot in

            //iterate over all the values and add them to a string
            var s = String()
            for child in snapshot.children {
              let name = child.value["Name"] as! NSString
              s += name + "\n" // the \n puts each name on a line
            }

            //add the string we just build to a textView
            let attrString = NSAttributedString(string: s)
            self.myTextView.textStorage?.appendAttributedString(attrString)

})

【讨论】:

  • 我真的很困惑。为了方便起见,请告诉我如何将它们检索到(文本视图)而不是(表格视图)中。
【解决方案2】:

使用您当前的节点“用户”,您必须下载所有用户并单独检查哪些用户的城市为“利雅得”。这将是一种浪费,因为您将读取大量您可能不需要的数据。

如果按城市搜索用户是您应用的主要功能,我将创建另一个节点“城市”,其中包含城市列表。然后,每个城市节点将包含该城市中所有用户的列表,您可以查询该节点。然后,如果您需要有关这些用户的更多信息,您知道要在“用户”节点中查找哪些特定人员。然后,您可以使用这些信息,但您认为适合您的表格视图。

Cities:
{
    "Riyadh": 
        {
          "07a5fa11-2a09-455b-92bf-a86dcd9d3e3e":true
        },
    "Dubai":
        {
          "08e5443c-cdde-4fda-8733-8c4fce75dd34":true
        }
},
Users:     
{
    "07a5fa11-2a09-455b-92bf-a86dcd9d3e3e":         
     {
        Name: "Hissah";
        Category: "Art & Designe";
        City: "Riyadh";
        Email: "H@him.fm";
        ShortDescription: "";
    };
    "08e5443c-cdde-4fda-8733-8c4fce75dd34":         
    {
        Name: "Sara";
        Category: "Cheefs";
        City: "Dubai";
        Email: "Sara@gmail.com";
        ShortDescription: "best cake ever . ";
    };

在此处进一步阅读,其中讨论了非规范化数据: https://www.firebase.com/docs/web/guide/structuring-data.html

【讨论】:

  • 很棒的数据结构。但是关于必须下载所有用户的部分是不正确的。 ref.queryOrderedByChild("City").queryEqualToValue("Riyadh") 可以解决问题。因此,虽然您的数据结构更好(因为不进行查询比进行查询便宜),但在这种情况下并不是严格需要的。
  • 当我通过添加另一个名为city的节点来使用上面的结构时,我如何检索“Dubai”城市中所有用户的(名称)?
  • 在我上面的示例中,您可以获得每个城市每个用户的 uid。然后,您可以使用这些 uid 从您的用户节点获取有关特定 uid 的更多信息。
  • @Hissah :所以,以迪拜为例。如果您检索节点“Cities/Dubai”的所有子节点,您将获得 Dubai 下的所有 uid,在本例中恰好是 08e5443c-cdde-4fda-8733-8c4fce75dd34。然后,您可以检索节点“Users/08e5443c-cdde-4fda-8733-8c4fce75dd34”,该节点将检索所有子信息。在这种情况下,这将是用户“Sara”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 2017-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多