0

I am trying to filter the posts based on their profile. For instance, when I go to my profile I only want to see my posts, not all the posts in my database. I attempted to make a filter for that but the code below does not seem to work and I am unsure as to why that is. It may be something obvious but I can not seem to pinpoint the issue, any ideas? I have attached a picture of the database to further assist anybody.

The code runs perfectly fine it just does not filter the usernames.

enter image description here

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var user = PFUser.currentUser()?.username!

    let bucketCellObj = tableView.dequeueReusableCellWithIdentifier("bucket", forIndexPath: indexPath) as! BucketTableViewCell

    var query = PFQuery(className: "Bucket")
    query.whereKey("creator", equalTo: user!)
    query.findObjectsInBackgroundWithBlock { (PFObject, error) -> Void in
        if error == nil {
            bucketCellObj.bucketname.numberOfLines = 0
            bucketCellObj.username.text = self.bucketsArray[indexPath.row]["creator"] as? String
            bucketCellObj.bucketname.text = self.bucketsArray[indexPath.row]["name"] as? String
            bucketCellObj.selectionStyle = .None
        } else {
            print("ERROR")
        }
    }
    return bucketCellObj
}
3
  • Sorry, just added it in the code above the query variable. Commented Jan 6, 2016 at 18:40
  • It looks like you're just storying the string of the name of the user in the creator column, so you'll need to use that as the param for the equalTo instead of the whole PFUser object. Just extract whatever field is being set as the creator. Also you probably should look into relations as that's the more standard way to link between two objects Commented Jan 6, 2016 at 18:43
  • 1
    Isn't the problem here the fact that you are fetching the objects async and returning the cell synchronously? Instead make a call in your viewDidLoad where you load the objects. Save them in an array, use that array as your data source and reload the tableview when you got the objects. Commented Jan 6, 2016 at 18:53

1 Answer 1

1

What you are doing here might work under some circumstances but will certainly bite you at some point.

What your code does:

  • show some arbitrary number of cells - probably based on self.bucketsArray.count or something similar
  • in each cell creation, run a parse query
  • when the query returns, customize the already displayed cell accordingly - without any usage of the requested query response

That will cause problems for a couple of reasons:

  • you perform too many requests, each cell requests some data, each new displayed cell requests its own data again
  • you display the old data in the cell until the new data is fetched which could take a few seconds due the amount of requests
  • you could encouter a problem where you requests some data for a cell, that cell moves off-screen, gets reused, then the first query returns, still holds the reference to it and will therefore display wrong data

How it can be solved

Do not requests the data in the cellForRowAtIndexPath.
Request the data once in viewDidLoad or similar. as soon as the data gets returned, parse it and initiate a tableView.reload().
In the cellForRowAtIndexPath make use of the already retrieved data, do not perform anymore async tasks.

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

Comments

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.