2

Here is my viewDidLoad method -

var query = PFUser.query()
    query.findObjectsInBackgroundWithBlock({ ( objects : [AnyObject]! , error: NSError! ) -> Void in

        //self.users.removeAll(keepCapacity: true)
        for object in objects {
            var user:PFUser = object as PFUser
            println(user.username)
            self.users.append(user.username)
        }
        self.tableView.reloadData()
    })
    println( " Count   \(users.count) ")

The count of users gets printed before the usernames in the users which makes me believe that it is taking time to fetch the users from the database. And for that reason my tableView never gets updated, the code for which looks like this -

var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

cell.textLabel?.text = users[indexPath.row]
    return cell

Gives me an 'Array index out of range error' because the number of rows in my table is three while my dictionary is empty.

Couldn't find any particular solution on swift. Any suggestions?

EDIT : Forgot to mention that the users do get printed but after a long time (even after the count of the users which are being printed after the usernames are)

Just for the information, count is always printed as 1.

The output is something like this - Count 1 genaks genaks1427 genaks14271 adu afd

3 Answers 3

2

I shifted my the code in my viewDidLoad function to the viewDidAppear function and it works just about the way I wanted it to work :)

Sign up to request clarification or add additional context in comments.

Comments

1

If you can, do the findObjectsInBackgroundWithBlock query inside the cellForRowAtIndexPath method. Check this tutorial: https://www.youtube.com/watch?v=Q6qcrO8uNzU he does the same in video.

2 Comments

I don't think that's a good idea because it will run a query each time the cellForRowAtIndexPath changes, which is unnecessary
You are absolutely right, I just did knew about this because of the tutorial.
0

When you said "the number of rows in my table is three" you inferred that in numberOfRowsInSection you returned 3.

If I'm right, I believe that's the problem.

You have to do it like this

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return users.count
}

This way the numberOfRowsInSection will never be different from the intended amount.

1 Comment

I guess the problem is that the time to fetch the users is too long and by the time the text for the cells of the table are being set, the data has not been fetched from the database. I say this because the users do get printed in my viewDidLoad method but even after the count gets printed which is obviously after the statement that prints the usernames.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.