gpt4 book ai didi

swift - Firebase,IN 子句查询和数据列表

转载 作者:搜寻专家 更新时间:2023-10-31 22:58:32 30 4
gpt4 key购买 nike

在我看来,当有人必须处理数据库时,这是一个非常明显的问题,所以我不知道为什么我找不到任何关于它的信息。
我正在尝试为我的项目设计非关系数据库。我有一个由一些信息(例如代码、图像和多语言描述)描述的产品列表,它们以不同的方式分类。这是结构的快照:

"products" : {
"-JiGh_31GA20JabpZBfa" : {
"code" : "IR3300000000",
"schema" : "http://lorempixel.com/400/200/",
},
"-JiGh_31GA20JabpZBfb" : {
"code" : "PJ0000000000",
"schema" : "http://lorempixel.com/400/200/",
}
}

"it" : {
"products" : {
"-JiGh_31GA20JabpZBfa" : {
"description" : "desc product IR330000000"
},
"-JiGh_31GA20JabpZBfb" : {
"description" : "description product PJ00000 lorem ipsum"
}
}
}

下面是一些产品的用法,真正的结构要复杂得多,但可能足以解释我的问题:

"families" : {
"family1" : {
"products" : {
"-JiGh_31GA20JabpZBfa" : true,
"-JiGh_31GA20JabpZBfb" : true
}
},
"family2" : {
"products" : {
"-JiGh_31GA20JabpZBfa" : true
}
}
}

"applications" : {
"application_1" : {
"products" : {
"-JiGh_31GA20JabpZBfa" : true,
"-JiGh_31GA20JabpZBfb" : true
}
},
"application_1" : {
"products" : {
"-JiGh_31GA20JabpZBfa" : true,
}
}
}

好的,现在让我们转到客户端!

我有几张表,一张显示系列,一张显示产品的应用。选择其中之一,我想显示属于该系列或应用程序中使用的所有产品。

这就是我正在努力解决的问题:如果我想在单元格中显示每个产品的描述和代码,我是否必须对每个产品执行查询以检索它们的字段,或者我可以使用某种排序“IN”查询?类似于 select * from products where key IN {"-JiGh_31GA20JabpZBfa", "-JiGh_31GA20JabpZBfa"}。我知道这是一个类似 SQL 的查询,但这只是为了更好地解释我自己。

顺便说一句,我不排除我设计数据库的方式是错误的。

最佳答案

正如@Frank van Puffelen 在第一条评论中指出的那样,没有办法执行 IN 查询,或者,我想,类似于 JOIN 的查询。< br/>下面是我的几段代码和我已实现的管道的简要说明!

我创建了 2 个 UITableViewController,第一个显示家庭,第二个显示所选的产品

因此,在第一个 Controller 中,我编写了这段代码来检索家庭列表:

let ref = FIRDatabase.database().reference()
ref.child("families").observeSingleEvent(of: .value, with: { snap in
if snap.value is NSNull { return }
let families = snap.value as! [String:AnyObject]
for familyKey in families.keys {
let familyDetails = familyes[familyKey] as! [String:AnyObject]
self.families.append(FamilyL1.init(familyName: familyKey, familyDetails: familyDetails))
}
self.tableView.reloadData()
})

选择一个系列后,我将在第二个 Controller 中执行两个查询:一个用于检索产品的详细信息,另一个用于多语言描述.

for (index, product) in (family?.products ?? []).enumerated() {
self.ref.child("products").child(product.id).observeSingleEvent(of: .value, with: { snap in
if snap.value is NSNull { return }
product.populateProduct(product: snap.value as! [String:AnyObject])
self.tableView.reloadRows(at: [IndexPath.init(row: index, section: 0)], with: .none)
})
self.ref.child("it").child("products").child(product.id).observeSingleEvent(of: .value, with: { snap in
if snap.value is NSNull { return }
product.populateProductDescription(product: snap.value as! [String:AnyObject])
self.tableView.reloadRows(at: [IndexPath.init(row: index, section: 0)], with: .none)
})
}

正如建议的那样,我利用了并行性,因此查询不会按顺序执行,并且感兴趣的单元格将在每次更新时刷新。我已经处理了可选值来实现这一点:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let product = self.family?.products[indexPath.row]
cell.textLabel?.text = product?.code ?? "..."
cell.detailTextLabel?.text = product?.pDescription ?? "..."
return cell
}

我希望这个回答能帮助那些正在尝试使用这个伟大平台的人!

关于swift - Firebase,IN 子句查询和数据列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40783240/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com