Search code examples
iosswiftparse-platformpfquery

Swift: Trying to displaying only the users data in one page


I've created a profile page to display all of the users blog post. In my viewController I've used a PFQueryTableViewControllerto query the users blogs. The problem I'm having is when I run my app to see the current users blogs, I see the current users post first in the order it was created, then under I see everyone else's blogs that is coming from the className I created in Parse. The post are also repeating when I'm on this page. Is there something I need to do in my code to only show the current signed in users blogs instead of all of the post from Parse? I've pasted my code below to get a better idea.

import UIKit
import Parse
import ParseUI

class ProfileVC: PFQueryTableViewController {

    var profilePage:NSMutableArray! = NSMutableArray()


    override init(style: UITableViewStyle, className: String!) {
        super.init(style: style, className: className)
    }


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.parseClassName = "BlogPost"
        self.textKey = "blogging"
        self.pullToRefreshEnabled = true
        self.objectsPerPage = 200

    }


    override func viewDidLoad() {
        super.viewDidLoad()

          let user = PFObject(className:"BlogPost")
           user["writer"] = PFUser.currentUser()




    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    override func objectsDidLoad(error: NSError?) {
        super.objectsDidLoad(error)
        profilePage = NSMutableArray(array: objects!)

        self.tableView.reloadData()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

            let query = PFQuery(className: "BlogPost")

        if let user = PFUser.currentUser() {
            query.whereKey("writer", equalTo: PFUser.currentUser()!.username!)
            query.addAscendingOrder("createdAt")
            query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in

                if error == nil{
                    for object in objects!{
                        let blog : PFObject = object as! PFObject
                        self.profilePage.addObject(blog)
                    }

                    let array : NSArray = self.profilePage.reverseObjectEnumerator().allObjects
                    self.profilePage = NSMutableArray(array: array)

                    self.tableView.reloadData()

                }
            }
        }
    }

    // MARK: - Table view data source

    override func objectAtIndexPath(indexPath: NSIndexPath!) -> PFObject? {
        var obj : PFObject? = nil
        if(indexPath.row < self.profilePage.count){
            obj = self.profilePage[indexPath.row] as? PFObject
        }

        return obj
    }


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

    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 110
        }
        else {
            return 90
        }
    }


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

        let cell = tableView.dequeueReusableCellWithIdentifier("PCell", forIndexPath: indexPath!) as! ProfileCell

        if let object : PFObject = self.profilePage.objectAtIndex(indexPath!.row) as? PFObject {

            cell.WritersName.text = object["writer"] as? String
            cell.WritersBlog.text = object["Blog"] as? String
            let dateCreated = object.createdAt! as NSDate
            let date = NSDateFormatter()
            dateFormat.dateFormat = "h:mm a"
            cell.timeProfile.text =  NSString(format: "%@", date.stringFromDate(dateCreated)) as String

        }
            return cell
        }



//    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

}

Solution

  • I can see a couple of potential problems with your code.

    First, you are adding to the same array every time you perform a query.

    If you initialize your array at the time you perform the query, the array will be limited to the current query's results and will not include results from previous fetches.

    To do this, add an initialization statement into the section where you are setting up your query.

    if let user = PFUser.currentUser() {
    
        profilePage = NSMutableArray() // Reset the profile page array.
    
        query.whereKey("writer", equalTo: PFUser.currentUser()!.username!)
        query.addAscendingOrder("createdAt")
        ...
    

    Additionally, the code in objectsDidLoad: may be overwriting the results that you are creating through your query.

    Try disabling the assignment to profilePage in there if you are still not getting the correct results.

    override func objectsDidLoad(error: NSError?) {
        super.objectsDidLoad(error)
        // profilePage = NSMutableArray(array: objects!)
        self.tableView.reloadData()
    }