我正在使用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 . ";
};
如何检索(城市)为"利雅得"的用户的(姓名)?到表视图?
提前致谢。
答案 0 :(得分:0)
使用您当前的节点“用户”,您必须下载所有用户并单独检查以查看哪些城市拥有“利雅得”。这将是一种浪费,因为您将阅读许多您可能不需要的数据。
如果按城市搜索用户是您应用的主要功能,我会创建另一个节点“城市”,其中包含城市列表。然后,每个城市节点将包含该城市中所有用户的列表,您可以查询该节点。然后,如果您需要有关这些用户的更多信息,您需要知道要在“用户”节点中查找哪些特定人员。然后,您可以使用此信息,但是您认为适合您的表格视图。
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
答案 1 :(得分:0)
在环中抛出这个,因为它是一个简单的答案,并解决了可用于填充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)
})