我尝试从Firebase提取数据,但是我认为我的代码做错了什么。如果您能帮助我,我将在下面分享我的提取方法和firebase结构
这是获取过程的第一部分
DispatchQueue.main.async {
Database.database().reference().child("Products/\(categoryUID)").observe(.value) { (snapshot) in
if let result = snapshot.children.allObjects as? [DataSnapshot] {
for child in result {
let companyId = child.key
self.getCompanyName(compID: companyId)
print(result)
}
}
}
}
公司成立后,我将使用第二种获取方法填充tableView
Database.database().reference().child("company").child(compID).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: Any] else {return}
self.company = Company(dictionary: dictionary, uid: compID)
self.data.append(self.company!)
print(self.data)
self.tableView.reloadData()
}) { (err) in
print("Failed to fetch user for posts:", err)
}
此过程耗时约15-20秒。我找不到问题的原因
这是火力基地的结构
"Products" : {
"-LCJzPPR6knojTMm3sqd" : {
"-LCJz95HuFlcrpGeRMa2" : {
"-LCK3ysCZTUG7rBBZuRS" : {
"product_detail" : "2325423542342",
"product_image_url" : [ "https://firebasestorage.googleapis.com/v0/b/e-fiyat-69e44.appspot.com/o/p%2F1526140758172-1.jpg?alt=media&token=8e9c3feb-c722-427a-98e3-c02a27607874" ],
"product_name" : "DENEME12",
"product_price" : "234"
}
}
},
"category" : {
"-LCJzPPR6knojTMm3sqd" : {
"imageUrl" : "https://firebasestorage.googleapis.com/v0/b/e-fiyat-69e44.appspot.com/o/category%2F1526139301086-Sandalye%20ve%20Koltuklar.png?alt=media&token=401ce15e-d08d-4487-9d79-67ec54e3f2b4",
"name" : "Sandalye & Koltuklar"
},
"company" : {
"-LCJz95HuFlcrpGeRMa2" : {
"imageUrl" : "https://firebasestorage.googleapis.com/v0/b/e-fiyat-69e44.appspot.com/o/company%2F1526139234155-cad.png?alt=media&token=9371db0c-d191-4277-93f6-871c43e758eb",
"name" : "Cadı"
},
虽然我正在编写此函数逻辑,但
我们需要访问“确定哪个公司拥有”类别的每个产品。
希望我能清楚地说明自己
答案 0 :(得分:0)
首先,我建议这样做:https://firebase.google.com/docs/database/ios/structure-data
如果我正确理解了您的需求,那么您的产品结构可能就是这样。但是您将获得主要思想。
"Products" : {
"products-uid" : {
"product_detail" : "2325423542342",
"product_image_url" : [ "https://firebasestorage.googleapis.com/v0/b/e-fiyat-69e44.appspot.com/o/p%2F1526140758172-1.jpg?alt=media&token=8e9c3feb-c722-427a-98e3-c02a27607874" ],
"product_name" : "DENEME12",
"product_price" : "234",
"companies": {
// the value here doesn't matter, just that the key exists
"company-one-uid": true,
"company-two-uid": true,
...
}
"categories": {
// the value here doesn't matter, just that the key exists
"cat-one-uid": true,
"cat-two-uid": true,
...
}
}
"companies" : {
"company-one-uid" : {
"imageUrl" : "https://firebasestorage.googleapis.com/v0/b/e-fiyat-69e44.appspot.com/o/company%2F1526139234155-cad.png?alt=media&token=9371db0c-d191-4277-93f6-871c43e758eb",
"name" : "Cadı"
}
"company-two-uid" : {...
}
}
"categories" : {
"cat-one-uid" : {....
然后,如果产品包含包含公司或类别的产品,则可以使用queryOrdered方法进行过滤。
答案 1 :(得分:0)
1)非常类似于在主线程中不会发生tableView重新加载的事实。 尝试为 self.tableView.reloadData()添加 DispatchQueue.main.async :
func updateCompanies() {
Database.database().reference().child("company").child(compID).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String: Any] else { return }
self.company = Company(dictionary: dictionary, uid: compID)
self.data.append(self.company!)
print(self.data)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}) { (err) in
print("Failed to fetch user for posts:", err)
}
}
2)另外,如果数据库中有很多项目,请尝试不要一次获取所有内容。使用queryLimitedToLast
Database.database().reference().child("Products/\(categoryUID)").queryLimited(toLast: 20).observe(.value) { ... }