0

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

JVS
  • 2,592
  • 3
  • 19
  • 31

3 Answers3

1

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!)")
                }
         }
Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
  • is this not a workaround? It seems like this is doing 2 queries instead of only one. queryForTable() just passes the query on to cellForRowAtIndexPath – JVS Aug 25 '16 at 13:01
  • I mean combined with the one in cellForRowAtIndexPath (it's a default behavior of PFQueryTableView) – JVS Aug 25 '16 at 13:02
0

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.
}
Santosh
  • 2,900
  • 1
  • 16
  • 16
0

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.

JVS
  • 2,592
  • 3
  • 19
  • 31