F. Bar F. Bar - 1 month ago 7
iOS Question

Load all data from parse before displaying table

I'm currently trying to populate a tableView with information stored on my Parse server (username, description, profile picture)

Currently... When I open the tableview I do not get any information showing, but if I click back and re open the table, all of my information shows and I'm unsure why that is.

A first get all information needed from Parse and store them into arrays like so:

let query = PFQuery(className: institutionTitle)
query.findObjectsInBackground {
(objects, error) in
if error == nil {
// The find succeeded.
// Do something with the found objects
if let objects = objects {
for object in objects {
self.postGrabber.append(object)
self.pSenderUsername.append(object["sender"] as! String)
self.pPostBody.append(object["textPost"] as! String)
}
}
} else {
// Log details of the failure
print("Error: \(error!)")
}
}


I'm currently using both the
viewDidLoad()
and
viewWillAppear()
to
reloadData()


I then set the delegate and datasource of the table view in the
viewDidLoad()
function.

I then use the cellForRowAtIndexPath method to set up each cell on the table:

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

cell.sender.text = pSenderUsername[indexPath.section]
cell.senderPost.text = pPostBody[indexPath.section]
cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.width / 2
cell.profilePicture.layer.borderColor = UIColor.orange.cgColor
cell.backgroundColor = UIColor.white

return cell
}


I'm looking for some information on how I would make the user wait until all data has successfully loaded and has been populated. Would appreciate some help.

Thanks in advance and feel free to request more information from myself.

Answer

You need to reload data in table view after you have fetched all from your server... Something like this should do the trick:

let query = PFQuery(className: institutionTitle)
query.findObjectsInBackground {
    (objects, error) in
    if error == nil {
        // The find succeeded.
        // Do something with the found objects
        if let objects = objects {
            for object in objects {
                self.postGrabber.append(object)
                self.pSenderUsername.append(object["sender"] as! String)
                self.pPostBody.append(object["textPost"] as! String)
            }
                dispatch_async(dispatch_get_main_queue(), {
                    self.tableView.reloadData() // Add this line and it should work
                })
        }
    } else {
        // Log details of the failure
        print("Error: \(error!)")
    }
}

If you want to load your data each time your view controller appears then do it in viewWillAppear(), otherWise, if you need to load data only first time, then in viewDidLoad()