Counting found objects in PFTableQueryViewController

43 Views Asked by At

I am trying to count the number of the found objects in PFQueryTableViewController.

I have tried working around with

override func queryForTable() -> PFQuery {
    let query = PFQuery(className: self.parseClassName!)
    query.whereKey("member", equalTo: memberId!)

    let count = query.countObjectsInBackground()
    label.text = "\(count)"


    return query

}

But my app will crash.

EDIT: The issue is not to make a query and count it's objects. The problem is to use queryForTable passing my query to cellForRowAtIndexPath of my PFQueryTableViewController

the cellForRowAtIndexPath looks like this:

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

    let cell:DetailApplicantCell = self.table.dequeueReusableCellWithIdentifier("reuseIdentifier") as! DetailApplicantCell

    if let name = object?.objectForKey(self.textKey!) as? String{
    cell.nameLbl.text = name
    }
    cell.groupImage.image = UIImage(named: "People.png")
    if let imageFile = object?.objectForKey(self.imageKey!) as? PFFile{
    cell.groupImage.file = imageFile
    cell.groupImage.loadInBackground()
    }

    return cell

}

NOTE that this is not the default cellForRow

3

There are 3 best solutions below

0
JVS On BEST ANSWER

Rather than doing a second PFQuery I found a better way using a method of PFQueryTableViewController like this:

    override func objectsDidLoad(error: NSError?) {
    super.objectsDidLoad(error)

    print("objectsDidLoad")
        if let results = self.objects{
        print("objectsFound")

        self.groupsCountLbl.text = "\(results.count)"
        self.groupsCountLbl.fadeIn()
    }
}

The VC has a property objects an array of AnyObject?. With the objectsDidLoad function you determine the time, everything is loaded.

2
Özgür Ersil On

Try with query.findObjectsInBackgroundWithBlock method and get the size() of the response object

        let query = PFQuery(className: self.parseClassName!)
        query.whereKey("member", equalTo: memberId!)
        query.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in

                if error == nil {
                    let count = objects.size()
                    label.text = "\(count)"
                    if let object = objects as? [PFObject] {

                    }
                } else {
                    // Log details of the failure
                    print("Error: \(error!)")
                }
         }
0
Santosh On

You are force unwrapping at 2 places, use if let:

func queryForTable() -> PFQuery? {
   if let parseClass = self.parseClassName {
      let query = PFQuery(className: parseClass)
      if let id = memberId {
         query.whereKey("member", equalTo: id)
      }

      let count = query.countObjectsInBackground()
      label.text = "\(count)"
      return query
   }
   return nil
}

Then you use your function like:

if let query = queryForTable() {
    //your query related code here.
}