How to query “Or ” with includeKey in parse?

老子叫甜甜 提交于 2019-12-08 10:39:49

问题


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")

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])

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


回答1:


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.

Like 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 = 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")

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

EDIT

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

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.

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.

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

EDIT 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")

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



来源:https://stackoverflow.com/questions/29364797/how-to-query-or-with-includekey-in-parse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!