0

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!!

  • Post the error here as text. Never post images if code or errors whenever possible. – Carcigenicate May 28 '17 at 17:11
  • 3
    And I found this after 5 seconds of searching: https://stackoverflow.com/questions/28804654/what-does-error-thread-1exc-bad-instruction-code-exc-i386-invop-subcode-0x0 – Carcigenicate May 28 '17 at 17:12
  • Okay, sorry. Here it goes: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) – cesar amnuel May 28 '17 at 17:13
  • @Carcigenicate it's possible that I'm not as good as searching exactly what I want as you xD – cesar amnuel May 28 '17 at 17:14
  • Please, post the data. Then we can show you exactly why your parsing is invalid. Most likely your JSON contains `null` for one of the attributes (e.g. `image`) or the data type is not what you expect to be. – Sulthan May 28 '17 at 17:14
  • 1
    @cesaramnuel Well, I mean, I literally just typed the error into Google. That should have been the first thing to try. – Carcigenicate May 28 '17 at 17:15
  • @Carcigenicate if I posted the question, is because I tried to find a solution and didn't. Moreover, the main problem was that I didn't actually know what was I exactly looking for. Thanks for your help =) – cesar amnuel May 29 '17 at 18:21
  • @cesaramnuel OK. Sorry if the way I phrased it sounded rude. My point was, when you get an error, first try to figure it out on your own of course, because you'll begin to learn the different types of errors over time. If you can't figure it out, just Google the literal error message that you get. 99% of all error messages you see will have existing answers somewhere (usually on this site already). To find the duplicate above, I just googled "Swift exc_bad_instruction". Including the language name helps if the error is common across languages. – Carcigenicate May 29 '17 at 18:32

0 Answers0