Parse, query to find games of one user - ios

In parse i have a "Game" class with array pointer column called "Players". I want to get games that contain [PFUser currentUser] in arrayKey called "Players", always this column stored a three players array.
I tried some query, i couldn't get success.
PFQuery *query = [PFQuery queryWithClassName:#"Game"];
[query whereKey:#"Players" containsAllObjectsInArray:#[[PFUser currentUser]]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{...}];
Also tried.
[query whereKey:#"Players" containsAllObjectsInArray:#[[PFUser currentUser].objectId]];
I'm sure already stored a Game with current User inside.
Thanks in advanced.

Just do this:
PFQuery *query = [PFQuery queryWithClassName:#"Game"];
[query whereKey:#"Players" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{...}];
If you run equalTo on an array key, it checks to see if the value is in the array.
Make sure to check that you stored a pointer to the user, and not their objectId. If you're storing id's instead, do equalTo:[PFUser currentUser].objectId];

Related

How to construct a PFQuery for locations?

I am constructing a PFQuery to get users within a radius(200 meters), i have set a Location column in Parse custom class in which i store geo locations. I get the users but when i calculate their distances manually through Haversine formula, it turns out to be more than 200 meters. This is my Pfquery:
PFQuery *query = [PFQuery queryWithClassName:#"UserLocations"];
[query whereKey:#"Location" nearGeoPoint:geopoint withinKilometers:0.2];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
compBlock(objects,error);
}];
What am i doing wrong?

IOS Parse include key query

I am trying to print the name of a category using Parse in iOS and Objectve-C. Thanks for looking. Let me know if you have any questions.
CategoryInBudget Table
category(pointer)
amount(number)
user(pointer)
Category Table
name(string)
Here is my code:
PFQuery *query = [PFQuery queryWithClassName:#"CategoryInBudget"];
[query whereKey:#"user" equalTo:[PFUser currentUser]];
[query includeKey:#"Category"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (PFObject *object in objects) {
NSLog(#"%#", object[#"Category"][#"name"];
}
}];

Parse ios wherekey using objectid

In my "Message" table in Parse I have a field called conversation, which is a pointer to a Conversation (another table in my database).
To query for a Message, can I do:
PFQuery *messageQuery = [PFQuery queryWithClassName:#"Message"];
[messageQuery whereKey:#"conversation" equalTo:_conversation.objectid];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];
or do I have to get the actual PFObject *myConversation and use that...
PFQuery *messageQuery = [PFQuery queryWithClassName:#"Message"];
[messageQuery whereKey:#"conversation" equalTo:myConversation];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];
It seems that #1 doesn't work, but #2 does...my question is how can I get #1 to work (i.e. use a PFObject's id to query when I have a pointer field)
.objectId is just a string, if your "conversation" key contains a pointer to myConversation, then you must include a PFObject in the equal to.
If you only have the objectId, you can search pointers without data using:
PFObject * myConversation = [PFObject objectWithoutDataWithClassName:#"Conversation" objectId:_conversation.objectid];
// continue here
[messageQuery whereKey:#"conversation" equalTo:myConversation];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];

Trying to query a PFRelation for a specific user in Parse

I'm trying to query a PFRelation of the current user of the app to load a table view showing all of the users friends. Each user is saved under a class called "User" when they first use the app and once they add a friend that friend is added to the column "friendsRelation" for the user. I'm using this code i got got from this link and nothing is returned from the query.
PFQuery *query = [PFQuery queryWithClassName:#"User"];
[query whereKey:#"friendsRelation" equalTo:[PFUser currentUser]];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(#"An error occurred fetching friends");
}
else {
[self.friends addObjectsFromArray:objects];
[self.tableView reloadData];
}
}];
Try this:
PFRelation *relation = [[PFUser currentUser] relationForKey:#"friendsRelation"];
PFQuery *query = [relation query];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
// results contains all friends of current user
}];
You are not query the PFUser table, so you cannot use [query whereKey:#"friendsRelation" equalTo:[PFUser currentUser]]; to query the User table, since the friendsRelation does not contain any PFUser objects but User objects. Just get the User object which represent for current user and use it to query the relation.
To Query the Users table don't use
[PFQuery queryWithClassName:#"User"];
Use instead
[PFUser query];
Only use the first if you have created your own "User" table and at this point if you checked the Data-browser you will see two different tables both called "User".

how to query based on pfobject/pfuser associated with another pfobject in parse.com?

I have a Parse class called FriendRelation. This class has two users, one a friend the other a user.
I want to get all of the messages posted by all of the friends of a user. I am attempting to do so with the following query:
PFQuery *innerQuery = [PFQuery queryWithClassName:#"FriendRelation"];
[innerQuery whereKey:#"user" equalTo:currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"Message"];
[query whereKey:#"userMessage" matchesQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
}];
This query comes back with no results.
I believe this is occurring because of the line:
[query whereKey:#"userMessage" matchesQuery:innerQuery];
The where key needs to be a FriendRelation to match. Is this correct?
How can I make the results of the inner query be a user that will intern match the matching query?
Thanks!
You can try using
- (void)whereKey:(NSString *)key matchesKey:(NSString *)otherKey inQuery:(PFQuery *)query
Something like:
PFQuery *innerQuery = [PFQuery queryWithClassName:#"FriendRelation"];
[innerQuery whereKey:#"user" equalTo:currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"Message"];
[query whereKey:#"userMessage" matchesKey:#"firendUser" inQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
//do something useful..
}];
Let me know how it goes!

Resources