2

This is my code in swift

class UserViewController: UITableViewController {

var userArray: [String] = []

@IBOutlet weak var friendListTableView: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

       retrieveMessages()

   }
func retrieveMessages() {
    var userArray: [String] = []
    var query:PFQuery = PFQuery(className: "User")
    var currentUser = query.whereKey("username", equalTo: PFUser.currentUser())
    currentUser.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in

        for object in objects! {
            let username:String? = (object as PFObject)["Friends"] as? String
            if username != nil {
                self.userArray.append(username!)
            }
        }
    }
    self.friendListTableView.reloadData()
}
            override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return userArray.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Update - replace as with as!

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = userArray[indexPath.row]

    return cell
}

This is my user's table https://www.dropbox.com/s/6xp48v3yn0l2hje/Screen%20Shot%202015-06-03%20at%202.10.13%20PM.png?dl=0
This is my current user's friend list in Parse Relation https://www.dropbox.com/s/pd8mt8sf35u1m0v/Screen%20Shot%202015-06-03%20at%202.10.55%20PM.png?dl=0

I've saved current user's friend list with PFRelation in class "User" in column "Friends" and I want to retrieve current user's friend list to show it in tableview but The problem is I can't update tableview to show current user's friend list, It's empty and there's no user list in tableview at all. Is my code correct for this method? If not please help me correct this code. Thank you!

sin90
  • 171
  • 1
  • 12

2 Answers2

0

You are updating the table before you receive the list from Parse, try this:

func retrieveMessages() {
    var userArray: [String] = []
    var query:PFQuery = PFQuery(className: "User")
    var currentUser = query.whereKey("username", equalTo: PFUser.currentUser())
    currentUser.findObjectsInBackgroundWithBlock {
        (objects, error) -> Void in

        for object in objects! {
            let username:String? = (object as PFObject)["Friends"] as? String
            if username != nil {
                self.userArray.append(username!)
            }
        }
        self.friendListTableView.reloadData()
    }
}

The only difference is that I move the reloadData function inside the completition block so it will happen after the data is returned from parse

Icaro
  • 14,585
  • 6
  • 60
  • 75
  • I did that and my tableview is still empty. What should I do next? Thank you – sin90 Jun 03 '15 at 01:51
  • If you did that and your table still empty your query is probably not returning any data, can you post a print screen of your User table from parse so we can investigate why? – Icaro Jun 03 '15 at 01:58
  • I've post a link in my question so you can check there. Thank you! – sin90 Jun 03 '15 at 07:23
0

You have to call reloadData from inside the findObjectsInBackgroundWithBlock block.

Right now you are calling reloadData before your data is fetched.