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

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

问题描述

因此,我在解析"和放置"模型中有事件"模型.每个模型都有地方.我也有用户,每个事件都有所有者. 因此,我需要获取事件或距我所在位置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.

编辑

解析文档的进一步阅读显示,复合查询不支持各种各样的东西...

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

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

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的东西用Java语言编写.

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天全站免登陆