-1

I am trying to fetch some data from the backend and then assign it to 3 different arrays. These arrays I then want to use to populate my tableViewCells. The issue is, when I print my arrays outside of the fetch block, they return nil. When I print them in the fetch block, they return the object's variables which I intend it to do so.

I include the self.tableView.reloadData() line in the hope that the arrays get populated and subsequently fill the tableViewCells, but it doesn't seem to be working.

Any suggestions welcomed on how to get those arrays populated correctly so when I print them outside of the fetch request they return the appropriate data.

var capArray = [String]()
var imageDic = [String: [PFFile]]()
var priceArray = [Int]()


override func viewDidAppear(animated: Bool) {

    capArray.removeAll(keepCapacity: true)
    imageDic.removeAll(keepCapacity: true)
    priceArray.removeAll(keepCapacity: true)

    let query = PFQuery(className: "SellerObject")
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

        if let objects = objects {
            for o in objects {

                if o.objectForKey("caption") != nil && o.objectForKey("imageFile") != nil && o.objectForKey("price") != nil {
                let cap = o.objectForKey("caption") as? String
                    self.capArray.append(cap!)
                let imdic = o.objectForKey("imageFile") as? [PFFile]
                self.imageDic[cap!] = imdic
                let price = o.objectForKey("price") as? String
                let priceInt = Int(price!)
                self.priceArray.append(priceInt!)

            }
            }   
        }
    }

   self.tableView.reloadData()     
}
Jamie Baker
  • 157
  • 6

1 Answers1

0

Your issue is self.tableView.reloadData() is executing before the block has finished executing.

The simplest thing to see it working is to move self.tableView.reloadData() inside the block, but that is a short term hack just to see things in action.

But that is not the correct long-term approach. To do this properly your array population code should really be in a model. Your table view would load initially with no data to use, your model would notify your view controller as data becomes available and the view controller then updates the table.

If the data comes from the device the table view could wait before it loads if the data can be retrieved quickly, if the data is coming over a network it needs to wait.

Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • OK, that makes sense. But now when I add the self.tableView.reloadData() the app crashes with the following error message:fatal error: unexpectedly found nil while unwrapping an Optional value – Jamie Baker Apr 09 '16 at 16:56
  • You need to find out where that's coming from. There's literally thousands of past questions with exactly the phrase "unexpectedly found nil while unwrapping an Optional value " in the title or question. – Gruntcakes Apr 09 '16 at 16:58
  • 1
    Check this thread: http://stackoverflow.com/questions/35683813/how-to-deal-with-fatal-error-unexpectedly-found-nil-while-unwrapping-an-option – Duncan C Apr 09 '16 at 17:11
  • Look for the "!". Anywhere you use an optional with forced unwrapping using ! is a suspect. – ryantxr Apr 09 '16 at 19:23