gpt4 book ai didi

ios - 如何从 Firestore 获取数据?

转载 作者:行者123 更新时间:2023-11-28 05:37:41 24 4
gpt4 key购买 nike

我正在尝试将数据从我的云 firestore 传递到我的 tableview,但它无法检索数据并将其发布到 tableview,到目前为止我的大部分尝试都失败了。因为我正在从实时数据库过渡到 Firestore。

我在堆栈上使用了多种资源,多次重组我的代码,现在归结为这个

这也是我在 Firestore 中收藏的图片 firestore collection

  import Foundation

class ProductList {
var id: String?
var name: String?
var dispensaryName: String?
var category: String?,
var brand: String?
var imageUrl: String?

init(id: String?,
name: String?,
dispensaryName: String?,
brand: String?,
category: String?,
imageUrl: String?) {

self.id = id
self.name = name
self.dispensaryName = dispensaryName
self.brand = brand
self.category = category,
self.imageUrl = imageUrl
}

}

  import UIKit

class ProductListCell: UITableViewCell {

@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var dispensaryName: UILabel!
@IBOutlet weak var productName: UILabel!
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var categoryStrain: UILabel!

}

  import UIKit
import Firebase
import FirebaseFireStore

class ProductListController: UIViewController {

@IBOutlet weak var productListTableView: UITableView!
@IBOutlet weak var menuButton: UIBarButtonItem!

var dbRef: DatabaseReference!

var productSetup: [ProductList] = []

override func viewDidLoad() {
super.viewDidLoad()

productListTableView.dataSource = self
productListTableView.delegate = self

self.productListTableView.rowHeight = UITableView.automaticDimension
self.productListTableView.estimatedRowHeight = 363

menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

dbRef = Database.database().reference().child("products");
//observing the data changes
dbRef.observe(DataEventType.value, with: { (snapshot) in

//if the reference have some values
if snapshot.childrenCount > 0 {

//clearing the list
self.productSetup.removeAll()

//iterating through all the values
for producting in snapshot.children.allObjects as! [DataSnapshot] {
//getting values
let productObject = producting.value as? [String: AnyObject]
let id = productObject?["id"]
let name = productObject?["name"]
let dispensaryName = productObject?["dispensaryName"]
let category = productObject?["category"]
let strain = productObject?["strain"]
let imageUrl = productObject?["imageUrl"]

//creating artist object with model and fetched values
let massProducts = ProductList(id: id as! String?,
name: name as! String?,
dispensaryName: dispensaryName as! String?,
category: category as! String?,
strain: strain as! String?,
imageUrl: imageUrl as! String?)

//appending it to list
self.productSetup.append(massProducts)
}

//reloading the tableview
print(self.productSetup)
self.productListTableView.reloadData()
}
})
}
}

extension ProductListController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productSetup.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductListCell") as!
ProductListCell

let production: ProductList
production = productSetup[indexPath.row]

cell.productName.text = "\(String(describing: production.brand)): \(String(describing: production.name))"
cell.dispensaryName.text = production.dispensaryName
cell.categoryLabel.text = production.category
cell.productImage.text = production.imageUrl
return cell
}
}

最佳答案

我已快速重新格式化代码以使其更易于理解,但这可能是许多事情之一;

  1. 检查在设备上使用 Firebase 进行身份验证的用户。
  2. 确保您已正确设置安全设置以允许在 Firebase 中进行读取。

重新格式化代码

ProductListController.swift

import Firebase

class ProductListController: UIViewController {

@IBOutlet weak var productListTableView: UITableView!
@IBOutlet weak var menuButton: UIBarButtonItem!

var productSetup = [ProductList]()

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}

override func viewDidLoad() {
super.viewDidLoad()
productListTableView.dataSource = self
productListTableView.delegate = self

productListTableView.rowHeight = UITableView.automaticDimension
productListTableView.estimatedRowHeight = 363

menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())

fetchProducts { (products) in
self.productSetup = products
self.productListTableView.reloadData()
}

}

func fetchProducts(_ completion: @escaping ([ProductList]) -> Void) {
let ref = Firestore.firestore().collection("products")
ref.addSnapshotListener { (snapshot, error) in
guard error == nil, let snapshot = snapshot, !snapshot.isEmpty else {
return
}
completion(snapshot.documents.compactMap( {ProductList(dictionary: $0.data())} ))
}
}

}

extension ProductListController: UITableViewDelegate, UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productSetup.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProductListCell") as?
ProductListCell else { return UITableViewCell() }

cell.configure(withProduct: productSetup[indexPath.row])

return cell
}

}

ProductListCell.swift

import Firebase

class ProductListCell: UITableViewCell {

@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var dispensaryName: UILabel!
@IBOutlet weak var productName: UILabel!
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var categoryStrain: UILabel!

func configure(withProduct product: ProductList) {
productName.text = "\(String(describing: product.brand)): \(String(describing: product.name))"
dispensaryName.text = product.dispensaryName
categoryLabel.text = product.category

fetchImage(withURL: product.imageUrl ) { (image) in
productImage.image = image
}
}

func fetchImage(withURL url: String, _ completion: @escaping (UIImage) -> Void) {
let ref = Storage.storage().reference(forURL: url)
ref.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
guard error == nil, let imageData = data, let image = UIImage(data: imageData) else {
return
}
completion(image)
}
}


}

ProductList.swift

class ProductList {
var id: String
var name: String
var dispensaryName: String
var category: String
var brand: String
var imageUrl: String

init(id: String, name: String, dispensaryName: String, brand: String, category: String, imageUrl: String) {

self.id = id
self.name = name
self.dispensaryName = dispensaryName
self.brand = brand
self.category = category
self.imageUrl = imageUrl
}

convenience init(dictionary: [String : Any]) {
let id = dictionary["id"] as? String ?? ""
let name = dictionary["name"] as? String ?? ""
let dispensaryName = dictionary["dispensaryName"] as? String ?? ""
let brand = dictionary["brand"] as? String ?? ""
let category = dictionary["category"] as? String ?? ""
let imageUrl = dictionary["imageUrl"] as? String ?? ""

self.init(id: id, name: name, dispensaryName: dispensaryName, brand: brand, category: category, imageUrl: imageUrl)
}

}

希望这对您有所帮助。

关于ios - 如何从 Firestore 获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58090786/

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