I have 2 classes: PFUser and Groups. In "Groups" class I have all my groups and each group has a relation key called "Members". "Members" holds a list of users related to current group.
How do I query for "Members" key so it returns an array of users?
I've done similar query in past for User related relations, but then I just passed in PFRelation *friendsRelation = [PFUser currentUser] objectForKey:#"friendsRelation"] into query.
This time can't get it to work.
The closest I've come is :
PFQuery *query = [PFQuery queryWithClassName:#"Groups"];
[query whereKey:#"Members" equalTo:[PFUser currentUser]];
[query orderByAscending:#"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {…}
p.s. Each user has his group(PFObject) added as a key.
How to do a PFRelation query for custom Class:
//1. Get objectID for object from custom class. Previously added as a key for user.
PFObject *currentGroup = [[PFUser currentUser] objectForKey:#"Group"];
//2. Set relation key for which to do the query.
PFRelation *relation = [currentGroup relationForKey:#"Members"];
PFQuery *membersQuery = [relation query];
//Do the query!
[membersQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {}];
There's no need to define for which class to do the query. ObjectIDs are unique.
Hope this will help someone.
If I understood your issue correctly, you want to have an array of « Members » in the same « Group » .
Your query is related to the class « Groups », it should be « Members » instead. You shouldn’t look for « members » equals to [PFUser currentUser], because it will always return only one item in the best case : the PFUser linked with your iPhone/iPad.
If your users has a group key (let’s name it « OwnerGroup » ), then you can try :
PFQuery *membersQuery = [PFQuery queryWithClassName:#"Members"];
[membersQuery whereKey:#"OwnerGroup" equalTo:currentGroup];
[membersQuery orderByAscending:#"username"];
[membersQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {…}
Related
If I have pointers to multiple _User objects in an array, can i query for a value of a User.
Ex.
I have a class Groups with an array members.
members = [user1, user2, user3].
Can I do something like:
PFQuery *query = [PFQuery queryWithClassName:#"Groups"];
[query whereKey:#"members.name" isEqual:#"James"];
This can be accomplished with a nested query. Create an "innerQuery" to find Users matching the criteria. Then constrain a Group query with whereKey:matchesQuery:.
PFQuery *innerQuery = [PFUser query];
// note that referring to the "name" field only makes sense if you've
// added a name field to User (you might mean username here)...
[innerQuery whereKey:#"name" isEqual:#"James"];
// now the main query is setup to match the innerQuery
PFQuery *query = [PFQuery queryWithClassName:#"Groups"];
[query whereKey:#"members" matchesQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
}];
I am designing a database. According to documentation, when number of relationships are greater than 100 and there is extra fields, I must design a Join Table. I designed this Join Table by having two pointer value.
This pointer value is pointing to _User. Later I need only rows for currUser.
This pointer value is the objectId of another table which is an entity. My question is, how can I write a query to return objects for this table in queryfortable.
Let's say:
Table _User
Table Entity
Table Join ---> objectId Pointer1(_User) Pointer2(Event)
This look like this:
This is what I have tried so far:
First I tried in viewDidLoad to get array of invitedUser from cloud and later in queryForTable:
PFQuery *query = [PFQuery queryWithClassName:#"Event"];
[query whereKey:#"objectId" containedIn:_inviteList];
but I need to access _inviteList.objectId which is not possible!
I tried to use innerQuerry or relation query. but as I just started learning parse I am not able to implement this.
PFUser *friendPointer = [PFUser currentUser];
PFQuery *query2 = [PFQuery queryWithClassName:#"Event"];
[query2 whereKey:friendPointer containedIn:_inviteList];
return query2;
This also did not work for me.
PFQuery *innerQuery = [PFQuery queryWithClassName:#"Invite"];
[innerQuery whereKey:#"invitedUser" equalTo:[PFUser currentUser]];
query = [PFQuery queryWithClassName:#"Event"];
[query whereKey:#"user" matchesQuery:innerQuery];
return query;
I appreciate if anyone can help me to write this query or re-design my table in order to have access to this query.
Pleaser try this code and give me review
PFUser *user = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"Invite"];
[query whereKey:#"invitedUser" equalTo:user];
[query includeKey:#"invitedUser"];
[query includeKey:#"eventId"];
[query orderByDescending:#"updatedAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (error == nil)
{
for (PFObject *underobject in [objects reverseObjectEnumerator])
{
PFUser *user1 = underobject[#"invitedUser"];
NSLog(#"invitedUser is :%#",user1);
}
}
}];
I have a class named Circle with a relation named "members" to the _User class.
Class Circle:
Class _User:
I'm trying to query all Circles that the current user belongs to (inside the "members" relations).
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:#"members"];
PFQuery *query = [relation query];
[query findObjectsInBackgroundWithBlock:^(NSArray *PF_NULLABLE_S objects, NSError *PF_NULLABLE_S error){
//objects size here is 0
//error is nil
}];
The problem is that the NSArray is empty and no error is received into the block.
One solution I'm thinking of is creating an actual table to store this relation and have a Relation column in both Circle and _User, but I believe there should be a better way to do this.
It doesn't appear that user has a members col. So, asking a user for its members relation is sure to fail. You want to query Circle...
PFQuery *query = [PFQuery queryWithClassName:#"Circle"];
[query whereKey:#"members" equalTo:user];
Having some trouble trying to figure out some logic for the following situation.
I have built a friends system for my mobile app using parse. Simply put, when a user "follows" something they are put into a relationship. That relationship contains all of the people that the individual user has folowed.
User
Relationship - Friends (contains all of the users that that overall user has followed)
I can query who an individual user is following fairly easily:
PFQuery *query = [PFUser query];
[query whereKey:#"username" equalTo:#"asg"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
for (PFObject *object in objects) {
PFRelation *friendsRelation = [object objectForKey:#"Friends"];
PFQuery *query = [friendsRelation query];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(#"%#", objects);
} else {
}
}];
}
}];
How would I query who is following a certain user, though? So, a users followers.
If you already know the user, use whereKey:equalTo::
PFObject *user = ...;
PFQuery *query = [PFUser query];
[query whereKey:#"Friends" equalTo:user];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
If you need to do a query to get the user then instead combine the requests with whereKey:matchesQuery::
[query whereKey:#"Friends" matchesQuery:userQuery];
I actually did a bunch of blog posts talking about implementing a friends list that might help you:
Overview
JavaScript part 1
JavaScript part 2
JavaScript part 3
The code samples are in JavaScript, but shouldn't be too hard to convert to iOS.
It covers the relationships, querying, updating etc.
The short answer to your question it to use a Join Table instead of a Relation so you can easily query from both sides of the relationship.
You can find a lot of information in the relations guide on the Parse site:
https://parse.com/docs/relations_guide#manytomany-jointables
I think what you are asking for is the PFUser *user = [PFUser currentUser] method. If you are trying to retrieve friends for the current user logged in. You could use the same for retrieving the users that the current user logged in has followed.
I'm trying to query to find specific users based on certain credentials.
The query will only look in the User class.
First, I want the query to match the currentUser's location
Second, must match a specific category (meaning multiple users in the same location may be under the same category)
Here's what I have tried:
self.profileObj = [self.currentUser objectForKey:#"city"];
PFQuery * queryOne = [PFUser query];
[queryOne whereKey:#"city" equalTo:self.profileObj];
PFQuery * queryTwo = [PFUser query];
[queryTwo whereKey:#"category" equalTo:self.tagString];
PFQuery * query = [PFQuery orQueryWithSubqueries:#[queryOne, queryTwo]];
[query orderByAscending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:(NSArrayobjects, NSError error)
This is returning 0 results.
You don't need two separate queries since you are querying on the same User object. Also, I believe you want two AND constraints rather than OR? If so, you can just chain together two whereKeys:
PFQuery * query = [PFQuery queryWithClassName:#"User"];
[query whereKey:#"city" equalTo:self.profileObj];
[query whereKey:#"category" equalTo:self.tagString];
[query orderByAscending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}];