如何查询“或"用 includeKey 解析? [英] How to query "Or " with includeKey in parse?

查看:21
本文介绍了如何查询“或"用 includeKey 解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在 Parse and Place 模型中有 Event 模型.每个模型都有地方.我也有用户,每个事件都有所有者.所以,我需要在我的位置周围 10 英里范围内获取我的事件或事件获取我使用的事件

So, I have Event model in Parse and Place model. Every model have Place. Also I have User, and every event have owner. So, I need to get my events, or events in 10 mile around of my location TO get my events I use

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

它有效,但现在我需要添加 OR 操作并查找 10 英里内的事件我用

It works, but now I need to add OR operation and find events in 10 miles I use

    let placeQuery = PFQuery(className: "Place")
    placeQuery.whereKey("location", nearGeoPoint: PFGeoPoint(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude), withinMiles: 20.0)

我需要如何进行主查询才能使用其中的两个?我试过了

And how I need to make main query to use two of this? I have tried

 var resultQuery:PFQuery = PFQuery.orQueryWithSubqueries([query, placeQuery])

但它给了我一个错误,即 orQueryWithSubqueries 需要使用相同的类

But it gives me an error, that orQueryWithSubqueries need to use the same class

推荐答案

目前,您有一个返回事件列表的查询,然后是一个返回地点列表的查询.

At the moment you have a query that returns a list of events and then a query that returns a list of places.

这就是您收到错误的原因.

This is why you are getting the error.

它们都需要返回相同的类型.然后你可以将它们或"在一起.

They both need to return the same type. Then you can "OR" them together.

像这样……

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)

只有这样,您才能在复合查询中包含键.包含键在子查询中使用时不起作用.

Only then do you include the keys on the compound query. Include key doesn't have an effect when used on a sub query.

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

这将返回一个事件列表,其中每个对象都填充了 Place 键.

This will now return a list of Events with the Place key populated in each object.

编辑

进一步阅读 Parse Docs 表明复合查询不支持各种东西...

Further reading of the Parse Docs shows that the compound queries do not support a variety of things...

但请注意,在复合查询的子查询中,我们不支持 GeoPoint 或非过滤约束(例如 nearGeoPoint、withinGeoBox...:、limit、skip、orderBy...:、includeKey:).

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.

看来您必须为此创建一个云函数.

It looks like you're going to have to create a cloud function for this.

使用云函数,您可以传入位置并运行两个单独的查询,然后在返回之前将它们组合成 now 数组.

Using a cloud function you can pass in the location and run two separate queries and then combine them into now array before returning it.

尽管使用 Cloud Code 的东西,但您必须用 Javascript 编写此代码.

You'll have to write this in Javascript though using the Cloud Code stuff.

编辑 2

其实你可以试试这个……

Actually, you could try this...

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

This might have the same limitations and not allow you to create it but it's worth a shot. :D

这篇关于如何查询“或"用 includeKey 解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆