如何在解析中使用 includeKey 查询 "Or "?
How to query "Or " with includeKey in parse?
所以,我在解析和放置模型中有事件模型。每个模型都有地方。我也有用户,每个事件都有所有者。
所以,我需要获取我的活动,或者我所在位置周围 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 需要使用相同的 class
目前您有一个查询 return 是一个事件列表,然后是一个 return 是一个地点列表的查询。
这就是您收到错误的原因。
他们都需要 return 相同的类型。然后你可以 "OR" 他们在一起。
像这样...
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")
这现在将 return 一个事件列表,每个对象中都填充了 Place 键。
编辑
进一步阅读 Parse Docs 表明复合查询不支持多种内容...
Note that we do not, however, support GeoPoint or non-filtering constraints (e.g. nearGeoPoint, withinGeoBox...:, limit, skip, orderBy...:, includeKey:) in the subqueries of the compound query.
看来您必须为此创建一个云函数。
使用云函数,您可以传入位置和 运行 两个单独的查询,然后在 return 之前将它们组合到 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
所以,我在解析和放置模型中有事件模型。每个模型都有地方。我也有用户,每个事件都有所有者。 所以,我需要获取我的活动,或者我所在位置周围 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 需要使用相同的 class
目前您有一个查询 return 是一个事件列表,然后是一个 return 是一个地点列表的查询。
这就是您收到错误的原因。
他们都需要 return 相同的类型。然后你可以 "OR" 他们在一起。
像这样...
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")
这现在将 return 一个事件列表,每个对象中都填充了 Place 键。
编辑
进一步阅读 Parse Docs 表明复合查询不支持多种内容...
Note that we do not, however, support GeoPoint or non-filtering constraints (e.g. nearGeoPoint, withinGeoBox...:, limit, skip, orderBy...:, includeKey:) in the subqueries of the compound query.
看来您必须为此创建一个云函数。
使用云函数,您可以传入位置和 运行 两个单独的查询,然后在 return 之前将它们组合到 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