所以,在Parse和Place模型中有事件模型。每个模特都有位置。我也有用户,每个事件都有所有者。
所以,我需要把我的活动,或在我所在地周围10英里内的活动
去拿我用过的东西

let query = Event.query()
        query.whereKey("author", containedIn: [PFUser.currentUser()!])
        query.includeKey("place")

可以,但现在我需要添加或操作并在10英里内查找事件
我用
    let placeQuery = PFQuery(className: "Place")
    placeQuery.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude), withinMiles: 20.0)

我需要如何进行主查询才能使用其中的两个?
我试过了
 var resultQuery:PFQuery = PFQuery.orQueryWithSubqueries([query, placeQuery])

但它给了我一个错误,即带子查询的orquery需要使用同一个类

最佳答案

现在您有一个返回事件列表的查询,然后是一个返回位置列表的查询。
这就是你犯错误的原因。
它们都需要返回相同的类型。然后你可以“或”他们在一起。
这样地。。。

let authorQuery = Event.query()
authorQuery.whereKey("author", containedIn: [PFUser.currentUser()!])

// note I'm using the "place.location" path to refer to the location key of the place key.
let placeQuery = Event.query()
placeQuery.whereKey("place.location", nearGeoPoint: geoPoint, withinMiles: 20.0)

只有这样,才能在复合查询中包含键。Include key在子查询上使用时不起作用。
let resultQuery:PFQuery = PFQuery.orQueryWithSubqueries([authorQuery, placeQuery])
resultQuery.includeKey("place")

现在将返回一个事件列表,每个对象中都填充了Place键。
编辑
Parse Docs的进一步阅读表明,复合查询不支持多种情况。。。
请注意,我们不支持复合查询的子查询中的GeoPoint或非过滤约束(例如nearGeoPoint、withinGeoBox…:、limit、skip、orderBy…:、includeKey:)。
看起来你必须为此创建一个云函数。
使用cloud函数可以传入位置并运行两个单独的查询,然后在返回之前将它们合并到now数组中。
你将不得不用Javascript编写,尽管你使用的是云代码。
编辑2
实际上,你可以试试这个。。。
let authorQuery = Event.query()
authorQuery.whereKey("author", containedIn: [PFUser.currentUser()!])

// note I'm using the "place.location" path to refer to the location key of the place key.
let placeQuery = Place.query()
placeQuery.whereKey("location", nearGeoPoint: geoPoint, withinMiles: 20.0)

let eventPlaceQuery = Event.query()
eventPlaceQuery.whereKey("place", matchesQuery: placeQuery)

let resultQuery:PFQuery = PFQuery.orQueryWithSubqueries([authorQuery, eventPlaceQuery])
resultQuery.includeKey("place")

这可能有相同的限制,不允许您创建它,但值得一试。:天

07-25 23:05
查看更多