F. Bar F. Bar - 1 month ago 8
Swift Question

PFQuery finding all objects within a class

This is a really basic question but I really don't understand why my code keeps looping through the query.

I have a parse server hosting by Heroku and a table view controller and I'm trying to create a query to return n number of rows (depending on how many rows on my table)

Here is the code I have made:

let query = PFQuery(className: institutionTitle)
query.findObjectsInBackground { (objects, error) in
if error != nil {
print("There has been an error")
print(error)
} else {
if let objects = objects {
for object in objects {
self.postGrabber.append(object)
}
self.tableView.reloadData()
}
}
}
print(postGrabber.count)


institutionTitle
is a string which matches with my
className
on Parse and
postGrabber
is an array of
[PFObject]
used to store the information from entries pulled from the table.

I have 1 row on my table but when I run this code and print
postGrabber.count
, for some reason it keeps incremented the number of rows in the array and I have no idea why.

If someone could help me it would be much appreciated.

Thanks,

Answer

If the line print(postGrabber.count) is called multiple times, the problem doesn't come from the query but from something higher in your code. If you don't find it copy paste your code here.