Swift Parse PFQuery filter tableA and filter pointer tableB

116 views Asked by At

here’s my concern:

I have a table A which contains a set of properties including a location property (FYI, no pointers in the table) I have a table B which contains a set of properties and a pointer pointing to a unique object in table A (using the table A objectId property). There may be a same pointer for several objects in table B. So basically, each object in table A has several objects pointing to it from table B.

What I’m trying to achieve is to filter all elements from table A within a certain location using: (please note generic names in the code examples)

let queryA = PFQuery(className: "tableA")
queryA.whereKey("location", nearGeoPoint: location, withinMiles: 5)

which works fine, and then to get all elements from table B pointing to the filtered elements .. and here is a first problem, because table A has no pointers, there seem to be no way to achieve this directly from table A, is there ?

So what I’ve tried is start filtering all objects in table B according to the filtered objects from table A, which led me to:

// Filtering all objects in table A according to a specific PFGeoPoint.
let queryA = PFQuery(className: "tableA")
queryA.whereKey("location", nearGeoPoint: location, withinMiles: 5)

// Filtering all elements in table B according to filtered elements from table A
 let queryB = PFQuery(className: "tableB")
 queryB.includeKey("pointer")
 queryB.whereKey("pointer", matchesKey: "objectId", in: queryA) 

// and then fetch the objects
queryB.findObjectsInBackground() { objects, error in
print(objects)
}

which doesn’t work because it seems that matchesKey must be operating under the same class.

I’ve then tried to find all objects from table A within a certain location using queryA and use a for-loop to iterate through the result of the method findObjectsInBackground with Block — [PFObject] — and at each iteration running another findObjectsInBackground using objectId from each PFObject element and passing it to queryB using whereKey and a

 PFObject(withoutDataWithClassName: <>, objectId: <>)

which works but leads to asynchronous problems and doesn’t seem quite proper to me.

Any ideas ?

Thanks.

1

There are 1 answers

11
danh On

First, make sure to model the relationship between tableB and tableA objects by assigning a relation to tableA (not an objectId) to tableB. In other words...

var tableAObject = PFObject(className:"tableA")
// setup tableAObject

var tableBObject = PFObject(className:"tableB")
// setup tableBObject
tableBObject["pointer"] = tableAObject  // not tableAObject["objectId"]

tableBObject.saveInBackground()  // this will save both objects

With that, you'll be able to make the relational query using whereKey:matchesQuery:...

let queryA = PFQuery(className: "tableA")
queryA.whereKey("location", nearGeoPoint: location, withinMiles: 5)

let queryB = PFQuery(className: "tableB")
queryB.whereKey("pointer", matchesQuery: queryA)
queryB.findObjectsInBackgroundWithBlock {}