For simplicity sake, I have two classes Location
and Program
. Location
has objectId
and name
, while Program
has objectId
, name
and location
(which is a pointer to Location
ObjectId
). When a user selects a location, how do I return the associated programs in a query? The following code doesn't work right now.
let pointer = PFObject(withoutDataWithClassName: "location", objectId: passedLocationID!) //passedLocationID is a string containing the ObjectId from the selected location
let query = PFQuery(className:"Program")
query.whereKey("location", equalTo: pointer.objectId!)
query.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = objects {
for object in objects {
self.programName.append(object["name"] as! String)
self.programID.append(object.objectId! as String)
}
}
} else {
}
This query prints the following error to the console "pointer field location needs a pointer value"
What would the correct code be to return the associated programs?
I have found a solution, but it seems like overkill. Two queries to Parse, I feel like this should be accomplished using only 1 query.
let firstQuery = PFQuery(className: "Location")
firstQuery.whereKey("objectId", equalTo: passedLocationID!)
let secondQuery = PFQuery(className: "Program")
secondQuery.includeKey("location")
secondQuery.whereKey("location", matchesQuery: firstQuery)
secondQuery.findObjects.......
So I found an answer.
When I pass the location to the program view controller, I need to pass it as a PFObject.
var passedLocationID: PFObject?
And then just a simple query works.
let newQuery = PFQuery(className: "Program")
newQuery.whereKey("location", equalTo: passedLocationID!)