【问题标题】:Swift Parse PFQuery filter tableA and filter pointer tableBSwift Parse Query 过滤表和过滤指针表
【发布时间】:2018-07-07 06:57:21
【问题描述】:

这是我的担忧:

我有一个表 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)
}

这不起作用,因为matchesKey似乎必须在同一个类下运行。

然后,我尝试使用 queryA 在某个位置从表 A 中查找所有对象,并使用 for 循环遍历方法 findObjectsInBackground 和 Block — [PFObject] — 的结果,并在每次迭代时运行另一个 findObjectsInBackground使用每个 PFObject 元素中的 objectId 并使用 whereKey 和 a 将其传递给 queryB

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

这可行,但会导致异步问题,对我来说似乎不太合适。

有什么想法吗?

谢谢。

【问题讨论】:

    标签: swift pointers parse-platform pfquery


    【解决方案1】:

    首先,确保通过将与 tableA 的关系(不是 objectId)分配给 tableB 来对 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 {}
    

    【讨论】:

    • 如果我错了,请纠正我(对此真的很陌生),我在最初的帖子中实现的查询将不起作用,因为我没有正确设置 tableB 和 tableA 之间的关系以前,对吧?因此,当您编写:tableBObject["pointer"] = tableAObject 时,字面意思是“指针属性下每一行的每个值都将引用 tableA(不是专门的 objectId)。当触发 saveInBackground() 时,这会做什么,替换值 in 指针属性?如果是这样,如何使用 "queryB.whereKey("pointer", matchesQuery: queryA)" 检索 tableA 中的特定行?谢谢。
    • 基本上,我需要在 tableB 的“指针”属性下替换所有引用 tableA 的 objectId 值?那么如何使用特定的过滤器获取匹配项?
    • @Julm - tableBObject["pointer"] = tableAObject 表示:“将对 tableA (tableAObject) 中特定对象的引用分配给 tableB (tableBObject) 中特定对象的'指针'属性”。隐含地,当tableBObject被保存时,它所引用的tableAObject也将被保存。
    • 但是如何让它们匹配呢?因为我们将整个表的引用分配给 tableBObject["pointer"] 而不是特定属性(例如 objectId)?换句话说,我在tableB中“指针”下的所有值都将具有与引用相同的tableAObject(整个表),对吗?
    • 我想我没有得到的部分是这个 tableAObject 代表什么。
    猜你喜欢
    • 2019-01-16
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多