Im trying to retrieve data set in Firebase from my firebase database and present it on the tableview.To present it forward and be able to view the data that I put into my firebase server
I have tried using multiple questions here on Stackoverflow but nothing seems to be working and the log error seems to be consistently the same Log Error
Firebase Tree
Products {
dfkjsv4qc22aWE:{
name:"Apples",
weight:"1 Bag",
price:"$5.99",
imageUrl:"https://firebasestorage.googleapis.com/v0/b/randomImageapp.BlahBlask2233"
}
se04Fws444:{
name:"Flower",
weight:"Bouquet",
price:"$10.99",
imageUrl:"https://firebasestorage.googleapis.com/v0/b/randomImageapp.BlahBlask2233"
}
}
import UIKit
import Foundation
import Firebase
import FirebaseStorage
import FirebaseDatabase
class ProductListController: UIViewController {
@IBOutlet weak var productListTableView: UITableView!
var feeds = [ProductList]()
var dbRef :DatabaseReference!
var storage : Storage!
var dbRef: DatabaseReference!
var storage: StorageReference!
fileprivate var products: [ProductList] = []
{
didSet
{
productListTableView.reloadData()
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
productListTableView.dataSource = self
productListTableView.delegate = self
dbRef = Database.database().reference()
storage = Storage.storage().reference()
readProducts()
printProducts()
}
var productsArrayDataSource = [ProductList]()
func readProducts() {
let productsRef = self.dbRef.child("products") //self.ref points to my firebase
productsRef.observe(.childAdded, with: { snapshot in
let aProduct = ProductList(withSnapshot: snapshot)
self.feeds.append(aProduct)
})
}
func printProducts() {
for product in self.feeds {
print(product.id!, product.name!)
}
}
extension ProductListController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return products.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ProductListCell") as!
ProductListCell
let details = products[indexPath.row]
cell.updateUI(with: details)
return cell
}
}
import Foundation
import UIKit
import Firebase
import FirebaseDatabase
class ProductList {
var name: String
var price: String
var weight: String
var imageUrl: String
init(withSnapshot: DataSnapshot) {
self.name = withSnapshot.childSnapshot(forPath: "name").value as? String ?? "No Name"
self.price = withSnapshot.childSnapshot(forPath: "price").value as? String ?? "No Price"
self.weight = withSnapshot.childSnapshot(forPath: "weight").value as? String ?? "No Weight"
self.imageUrl = withSnapshot.childSnapshot(forPath: "imageUrl").value as? String ?? "No ImageUrl"
}
}
import UIKit
import SDWebImage
import Firebase
class ProductListCell: UITableViewCell {
@IBOutlet weak var productImage: UIImageView!
@IBOutlet weak var productName: UILabel!
@IBOutlet weak var weight: UILabel!
@IBOutlet weak var price: UILabel!
func updateUI(with productList: ProductList) {
productName.text = productList!.name
price.text = productList!.price
weight.text = productList!.weight
productImage.text = productList!.imageUrl
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
Edit: this is what I'm getting so far in my errors error 2 it seems like its more a firebase problem than my coding error