I'm trying to make a simple iOS app (most recent version of Xcode, Swift 3). All it has to do is to download some kind of objects from a Firebase DB, and generate a dynamic list with all that info. I ensured the app can download all the info properly, and certainly does, but when time to generate de TableView arrives, it fails catastrophically. here is the code:
var ref: DatabaseReference!
var refHandle:UInt!
var productList = [Product]()
@IBOutlet weak var myTable: UITableView!
let cellId = "Product"
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
myTable.delegate = self
myTable.dataSource = self
fetchProducts()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productList.count
}
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath as IndexPath) as! ProductTableViewCell
cell.nameLabel.text = productList[indexPath.row].name
cell.priceLabel.text = "" + (productList[indexPath.row].price?.description)!
let imageDict:NSDictionary = productList[indexPath.row].imageName!
let imageDownloaded = downloadImageProductFromFirebase(append: imageDict["hash"] as! String)
cell.picture.image = imageDownloaded
return cell
}
func fetchProducts(){
self.productList.removeAll()
refHandle = ref.child("products").observe(.childAdded, with: {(snapshot) in
print(snapshot)
var i = 0
for element in snapshot.children
{
i = i + 1
let item:DataSnapshot = element as! DataSnapshot
let key = item.key
let productsRef = self.ref!.child("products").child(key)
productsRef.observe(.value, with: { snapshot in
let productItem = (snapshot ).value as? [String : AnyObject]
let product = Product(name: productItem?["name"] as! String, image: productItem?["image"] as! NSDictionary, price: productItem?["price"] as! Float)
self.productList.append(product)
self.tableView.reloadData()
})
}
})
}
func downloadImageProductFromFirebase(append:String) -> UIImage{
let storage = Storage.storage()
var image = UIImage ()
var reference: StorageReference!
//all I did here was remove self before storage
reference = storage.reference(forURL: "gs://fridgeapp-3e2c6.appspot.com/productImages/" + append)
reference.downloadURL { (url, error) in
if(error != nil){
let data = NSData(contentsOf: url!)
image = UIImage(data: data! as Data)!
}
else {
image = #imageLiteral(resourceName: "ic_store")
}
}
return image
}
Moreover, I took a pic of the error shown (it says nothing on console, just this):Image of the error
Needless to say I'm totally new to Swift programming, and I've search all over the Internet, and nothing... Any kind soul that helps me? Thanks in advance!!