这是我的担忧:
我有一个表A,其中包含一组属性,包括位置属性(仅供参考,表中没有指针) 我有一个表B,它包含一组属性和一个指向表A中唯一对象的指针(使用表A的objectId属性)。表B中的几个对象可能有相同的指针。 因此,基本上,表A中的每个对象都有几个从表B指向它的对象。
我要实现的目标是使用以下方法过滤表A中某个位置的所有元素(请注意代码示例中的通用名称)
let queryA = PFQuery(className: "tableA")
queryA.whereKey("location", nearGeoPoint: location, withinMiles: 5)
工作正常,然后从表B中获取所有指向已过滤元素的元素..这是第一个问题,因为表A没有指针,所以似乎没有办法直接从表A中实现,在吗?
所以我尝试根据表A中过滤的对象开始过滤表B中的所有对象,这导致我:
// 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)
}
不起作用,因为似乎matchsKey必须在同一类下运行。
然后,我尝试使用queryA在某个位置从表A中查找所有对象,并使用for循环遍历带有Block [PFObject]的findObjectsInBackground方法的结果,并在每次迭代中运行另一个findObjectsInBackground使用每个PFObject元素中的objectId并使用whereKey和a
将其传递到queryB PFObject(withoutDataWithClassName: <>, objectId: <>)
可以工作,但是会导致异步问题,对我来说似乎不太合适。
有什么想法吗?
谢谢。
答案 0 :(得分:0)
首先,通过为tableB分配与tableA(不是objectId)的关系,确保对tableB和tableA对象之间的关系进行建模。换句话说...
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
这样,您就可以使用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 {}