Parse subQuery filter swift ios - ios

I am running a PFQuery on the main _User table (ACL is read-only if the user isn't the PFUser.currentUser).
I am logging a count for profile views.
Since the PFUSer table cannot be incremented by anyone other than the currentUser, I have to store the count in a separate table. Call this table "ViewCount," with pointer column "userId" that points back to the PFUser table.
How can I filter a query on the main PFUser table for users with a given count from the ViewCount table?????
let query = PFUser.query()!
//perform filters here
query.findObjectsInBackgroundWithBlock({

You can do this with a subquery.
let query = PFUser.query()!
let subQuery = PFQuery(className: "viewCount")
subQuery.whereKey("count", greaterThan: yourCount)
query.whereKey("userCount", matchesQUery: subQuery)
...
//rest of query info

Related

Swift: Multiple Parse Query from one class

I have a Parse Class named friendRequest where keys are requestFrom & requestTo which contains unique ids of people who have sent request and received them respectively.
I have a user for whom I need to fetch friend requests which have been sent to him and received by him to find out the status of the request.
I am trying to combine two queries where I match the user's id in requestFrom & requestTo keys and fetch all the results where the condition is true. I get 0 values in return.
My code is:
let friendsQuery : PFQuery = PFQuery(className: "friendRequest")
let objectId = PFUser.current()?.objectId
friendsQuery.whereKey("requestFrom", equalTo: objectId!)
friendsQuery.whereKey("requestTo", equalTo: objectId!)
friendsQuery.findObjectsInBackground(block: { (objects, error) in
//code
})
How should I combine the queries to get the desired results?

using a friend system in ios parse swift

I created a query with subquery
var userInitiated = PFQuery(className: "friends")
userInitiated.whereKey("friender", equalTo: PFUser.currentUser()!.username!)
var friendInitiated = PFQuery(className: "friends")
friendInitiated.whereKey("friendee", equalTo: PFUser.currentUser()!.username!)
// find friends of user
let friendQuery = PFQuery.orQueryWithSubqueries([userInitiated, friendInitiated])
friendQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) in
if error == nil {
// cleanup
self.friendArray.removeAll(keepCapacity: false)
// STEP 2 Hold Recieved Data
// find objects that you queried for
for object in objects! {
self.friendArray.append(object.valueForKey("-----") as! String)
}
}
})
In the for object in objects part - I want to append the usernames that I got from the query but I only want to add the ones that aren't the current users username, how would I do that?
A is current User, B is one of A's friends. So what the (friender, friendee) pair in your "friends" class?
Both (A,B), (B,A)
one of (A,B) and (B,A)
In first case, you can just query friender equalTo currentUser
In second case, your query seems ok.

Parse.com PFUser Query based on results from PFObject query

I have a chatroom class containing users. I can successfully query that class and return only specific PFUsers.
// get all room IDs associated with current user
var outerRoomQuery = PFQuery(className:Parse.CHATROOM_CLASS_NAME)
outerRoomQuery.whereKey(Parse.CHATROOM_USER, equalTo: PFUser.currentUser()!)
outerRoomQuery.selectKeys([Parse.CHATROOM_ROOMID])
// get all users associated with above roomIds ignoring current User
var innerRoomQuery = PFQuery(className:Parse.CHATROOM_CLASS_NAME)
innerRoomQuery.whereKey(Parse.CHATROOM_ROOMID, matchesKey:Parse.CHATROOM_ROOMID, inQuery: outerRoomQuery)
innerRoomQuery.whereKey(Parse.CHATROOM_USER, notEqualTo: PFUser.currentUser()!)
innerRoomQuery.includeKey(Parse.CHATROOM_USER)
innerRoomQuery.orderByDescending(Parse.CHATROOM_UPDATEDACTION)
innerRoomQuery.selectKeys([Parse.CHATROOM_USER])
The above query returns about 5 users.
I'm wondering if it is possible to combine these results with a separate PFUser.query() to create an OR query something like this:
var userQuery = PFUser.query()
userQuery?.whereKey(Parse.CHATROOM_USER, matchesKey:Parse.CHATROOM_USER, inQuery: innerRoomQuery) // returns nil
var receptionQuery = PFUser.query()
receptionQuery?.whereKey(Parse.USER_ROLE, equalTo:Parse.PFROLE_RECEPTIONIST) // returns around 10 receptionist users
var orUserQuery = PFUser.query()
return PFQuery.orQueryWithSubqueries([userQuery!, receptionQuery!])
The result of above only returns the receptionists. So my question is how to achieve this line:
userQuery?.whereKey(Parse.CHATROOM_USER, matchesKey:Parse.CHATROOM_USER, inQuery: innerRoomQuery)
I know this is wrong because there is no "user" (Parse.CHATROOM_USER) field in the PFUser class. I'm just wondering if there is a way to do this without firing off the roomQuery first and putting the returned PFUsers into an array and then firing the receptionist users query and combining the arrays.
Any ideas?
I'm not entirely sure what you are asking but if you are trying to have a single query find a specific type of user, i would approach this in a different fashion. I would add a bool type to the user class for what I am searching for, and just query for all the users with that bool type as true!

ios swift parse: Having general issues with deep queries and their result data

I am still having problems understanding the correct way of handling deeper pointer structure in parse.
Example:
Card has pointer to CardSet
CardSet has pointers to Lesson and User
Lets say, I want to have all CardSets including
Lesson.name
Count of Cards for each CardSet
Can I query all this in just one query?
And have the data available without any additional fetchIfNeededInBackgroundWithBlock queries?
I know that I can get the Lesson with
var query = PFQuery(className: "CardSet")
query.whereKey("user", equalTo: PFUser.currentUser())
query.includeKey("lesson")
But that gives me only the lesson object, I can not access any data (like the col "name") from this class unless I use fetchIfNeededInBackgroundWithBlock what takes another query and of course more time to load.
What can I do to have all queried data
including all pointers columns
in order to pin this data to the local datastore with
PFObject.pinAllInBackground(objects, block: nil)
And not to forget, how can I query the number of cards related to the CardSet?
First I would recommend subclassing in Parse, it makes relationships between tables (pointers etc) clearer.
How to subclass the PFObjects you can explore this guide on parse
How to query the number of cards in CardSet
You need the CardSet from which you want the cards. (PFObject or subclassed PFObject).
Then just do this:
var query = PFQuery(classname: "cards")
query.whereKey("CardSet", equalTo: yourCardSetObject)
//Synchronously
query.findObjects().count
//Asynchronously
query.findObjectsInBackgroundWithBlock({
(objects, error) in
if error != nil {
println(error.localizedDescription)
return
}
objects.count
})
How to get the name of the lesson
As I said, it's recommended to subclass the PFObjects because you need to cast the objects what isn't really funny to debug and is horrible code.
I did it that way:
var query = PFQuery(className: "CardSet")
query.whereKey("user", equalTo: PFUser.currentUser())
query.includeKey("lesson")
query.findObjectsInBackgroundWithBlock({
(objects, error) in
if error != nil {
println(error.localizedDescription)
return
}
for object in objects as? [PFObject] {
var lesson = object["lesson"] as PFObject
println(lesson["name"])
}
})

Parse query not passing on PFObject pointer relation

So, what I have is a table named Votes, table User and table Venue. I have pointer relation in Votes to User and Venue. Venue is subclassing PFObject. This is the way I set the objects to the database:
let votes = PFObject(className: "Votes")
votes["venue"] = venue
votes["user"] = PFUser.currentUser()
And this is working as expected, I can see both objects in the dashboard. But when I try to fetch a Votes row, the Venue object is not passing the query. Any ideas why this is happening. The query:
let user = PFUser.currentUser()
let query = PFQuery(className: "Votes")
query.whereKey("venue", equalTo: venue)
query.whereKey("user", equalTo: user)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock(...)

Resources