因此,我在解析和放置模型中具有事件模型。每个模型都有地方。我也有用户,每个事件都有所有者。因此,我需要获取事件或位置附近10英里内的事件才能获取我使用的事件
let query = Event.query()
query.whereKey("author", containedIn: [PFUser.currentUser()!])
query.includeKey("place")
它有效,但是现在我需要添加OR操作并在我使用的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])
但这给我一个错误,即orQueryWithSubqueries需要使用相同的类
目前,您有一个查询,该查询返回一个事件列表,然后一个查询,返回一个地点列表。
这就是为什么您得到此错误。
它们都需要返回相同的类型。然后,您可以将它们“或”在一起。
像这样...
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)
只有这样,您才能在复合查询中包括关键字。包含键在子查询上使用时不起作用。
let resultQuery:PFQuery = PFQuery.orQueryWithSubqueries([authorQuery, placeQuery])
resultQuery.includeKey("place")
现在,这将返回事件列表,并在每个对象中填充位置键。
编辑
对Parse Docs的进一步阅读显示,复合查询不支持多种内容。
但是请注意,我们在复合查询的子查询中不支持GeoPoint或非过滤约束(例如,nearGeoPoint,innerGeoBox ...:,limit,skip,orderBy ...:,includeKey :)。
看来您将不得不为此创建一个云功能。
使用云函数,您可以传递位置并运行两个单独的查询,然后将它们合并到now数组中,然后再返回。
您必须使用Cloud Code来用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")
这可能有相同的局限性,不允许您创建,但是值得一试。:D
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句