从firestore中读取数据并显示在collectionView中
Read data from firestore and display in collectionView
我有:
业务类别结构
struct BusinessCategory: Codable, Identifiable {
@DocumentID var id: String?
var name: String?
var image: String?
enum CodingKeys: String, CodingKey {
case id
case name
case image
} }
正在创建一些类别:
var categories: [BusinessCategory] = [
.init(id: "id1", name: "Doctor", image: "https://icon.url"),
.init(id: "id2", name: "Restaurant", image: "https://icon.url"),
.init(id: "id4", name: "Citizen Service", image: "https://icon.url"),
.init(id: "id5", name: "Barber Shop", image: "https://icon.url"),
.init(id: "id6", name: "Corona-Test", image: "https://icon.url")
]
显示它们:
switch collectionView{
case CategoryCollectionView:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CategoryCollectionViewCell.identifier, for: indexPath) as! CategoryCollectionViewCell
cell.setup(category: categories[indexPath.row])
return cell
default: return UICollectionViewCell()
}
我需要:
如何显示存储在 firestore 数据库中的 ALL businessCategories?我的 Firestore 结构如下所示:
我有一个函数可以获取存储在firestore中的所有文档,但我不知道如何显示文档数据。更准确地说,我需要提取并显示在 collectionView 中的“名称”和“url”。
func getAllCategories(){
database.collection("categories").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.data())")
}
}
}
}
入门指南中介绍了如何从 Firestore 获取数据Read Data
简而言之,有两个选项(或更多);从文档中获取每个谨慎的字段
for doc in querySnapshot!.documents {
let name = doc.get("name") as? String ?? "No Name"
}
或使用 codable
将数据作为整个对象读取的首选方式
for doc in querySnapshot!.documents {
let cat = try doc.data(as: BusinessCategory.self)
}
这里有一个很好的使用 Firestore 和可编码对象的指南
Mapping Firestore Data in Swift
这是一本非常好的读物,将为您节省大量时间(和代码行!)
我有: 业务类别结构
struct BusinessCategory: Codable, Identifiable {
@DocumentID var id: String?
var name: String?
var image: String?
enum CodingKeys: String, CodingKey {
case id
case name
case image
} }
正在创建一些类别:
var categories: [BusinessCategory] = [
.init(id: "id1", name: "Doctor", image: "https://icon.url"),
.init(id: "id2", name: "Restaurant", image: "https://icon.url"),
.init(id: "id4", name: "Citizen Service", image: "https://icon.url"),
.init(id: "id5", name: "Barber Shop", image: "https://icon.url"),
.init(id: "id6", name: "Corona-Test", image: "https://icon.url")
]
显示它们:
switch collectionView{
case CategoryCollectionView:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CategoryCollectionViewCell.identifier, for: indexPath) as! CategoryCollectionViewCell
cell.setup(category: categories[indexPath.row])
return cell
default: return UICollectionViewCell()
}
我需要: 如何显示存储在 firestore 数据库中的 ALL businessCategories?我的 Firestore 结构如下所示:
我有一个函数可以获取存储在firestore中的所有文档,但我不知道如何显示文档数据。更准确地说,我需要提取并显示在 collectionView 中的“名称”和“url”。
func getAllCategories(){
database.collection("categories").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
print("\(document.data())")
}
}
}
}
入门指南中介绍了如何从 Firestore 获取数据Read Data
简而言之,有两个选项(或更多);从文档中获取每个谨慎的字段
for doc in querySnapshot!.documents {
let name = doc.get("name") as? String ?? "No Name"
}
或使用 codable
将数据作为整个对象读取的首选方式for doc in querySnapshot!.documents {
let cat = try doc.data(as: BusinessCategory.self)
}
这里有一个很好的使用 Firestore 和可编码对象的指南
Mapping Firestore Data in Swift
这是一本非常好的读物,将为您节省大量时间(和代码行!)