根据(城市)从firebase检索数据



我正在使用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 . ";
    };

如何将(城市)为"利雅得"的用户的(名称)检索到表视图?

提前谢谢。

将其放入环中,因为这是一个简单的答案,并解决了可用于填充表的dataSource查看

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)
})

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

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

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

最新更新