Multiple whereKey:doesNotMatchKey:inQuery: in one query - ios

I have users friendships sorted in a Friendship class with fromUser and toUser keys. When I want to only return users that are not in a friendship with the currentUser, I want to use two whereKey:doesNotMatchKey:inQuery: to sort the current friendships out.
PFQuery *query = [PFUser query];
[query whereKey:#"objectId" notEqualTo:[PFUser currentUser].objectId];
[query whereKey:#"username" hasPrefix:string];
PFQuery *fromFriends = [PFQuery queryWithClassName:parseFriendshipClassName];
[fromFriends whereKey:parseFriendshipFromUserKey equalTo:[PFUser currentUser]];
-> [query whereKey:#"objectId" doesNotMatchKey:parseFriendshipToUserIdKey inQuery:fromFriends];
PFQuery *toFriends = [PFQuery queryWithClassName:parseFriendshipClassName];
[toFriends whereKey:parseFriendshipToUserKey equalTo:[PFUser currentUser]];
-> [query whereKey:#"objectId" doesNotMatchKey:parseFriendshipFromUserIdKey inQuery:toFriends];
If I comment out either of these two lines, the other one works as expected, but if they are both there, only the last of them works.
In the Friendship table, the users are referenced both as a pointer and as their objectId. I use the objectId to reference them here, so it is not because I compare pointers to ids.
The documentation does not seem to suggest, that adding more than one would override the old one.
Is this expected behaviour? Or am I doing something wrong?

In this case you should use the orQueryWithSubqueries:. It should solve this.

Related

Parse - Query a value in a pointer in an array

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) {
}];

How to access fields of a pointer's value on parse.com with iOS?

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

Parse iOS - How to query PFUser details from another class

I am trying to write a Parse query for my iOS social app that will show all users that are following the active user. Here is what I have so far:
PFQuery *followingUser = [PFQuery queryWithClassName:#"Activity"];
[followingUser whereKey:#"Activity" equalTo:#"follow"];
[followingUser whereKey:#"fromUser" equalTo:self.user];
[followingUser includeKey:#"User"];
[followingUser setCachePolicy:kPFCachePolicyCacheThenNetwork];
Currently this correctly gives me all the records of the active user following any other users, But this doesn't give me the PFUser details (user profile pic, display name, etc) for the corresponding records. Any ideas on how i can easily draw out the PFUser details in the one query?
Thanks in advance
Here is some sample code of a query that I used to get all the users being followed, all posts from the users being followed, and all posts from the current user. I hope this helps!
// List of all users being followed by current user
PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:kFTActivityClassKey];
[followingActivitiesQuery whereKey:kFTActivityTypeKey equalTo:kFTActivityTypeFollow];
[followingActivitiesQuery whereKey:kFTActivityFromUserKey equalTo:[PFUser currentUser]];
followingActivitiesQuery.cachePolicy = kPFCachePolicyNetworkOnly;
followingActivitiesQuery.limit = 100;
// Posts from users being followed
PFQuery *postsFromFollowedUsersQuery = [PFQuery queryWithClassName:self.parseClassName];
[postsFromFollowedUsersQuery whereKey:kFTPostUserKey matchesKey:kFTActivityToUserKey inQuery:followingActivitiesQuery];
[postsFromFollowedUsersQuery whereKey:kFTPostTypeKey containedIn:#[kFTPostTypeImage,kFTPostTypeVideo,kFTPostTypeGallery]];
// Posts from current user
PFQuery *postsFromCurrentUserQuery = [PFQuery queryWithClassName:self.parseClassName];
[postsFromCurrentUserQuery whereKey:kFTPostUserKey equalTo:[PFUser currentUser]];
[postsFromCurrentUserQuery whereKey:kFTPostTypeKey containedIn:#[kFTPostTypeImage,kFTPostTypeVideo,kFTPostTypeGallery]];
PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects: postsFromFollowedUsersQuery, postsFromCurrentUserQuery, nil]];
[query includeKey:kFTPostUserKey];
[query orderByDescending:#"createdAt"];

Multiple Query Search with Parse.com

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) {
...
}];

or query with include

I have a join table with a from column and a to column. Both columns point to user objects. When perform this OR query, it throws the error: 'OR queries do not support sub-queries with includes'
Is there any way around this? Thanks!
// set up query to get all relationships where the current user friended another user.
PFQuery *userIsFriendSender = [PFQuery queryWithClassName:#"UserRelationships"];
[userIsFriendSender whereKey:#"from" equalTo:[PFUser currentUser]];
[userIsFriendSender whereKey:#"active" equalTo:#YES];
[userIsFriendSender includeKey:#"to"];
// set up query to get all relationships where a use friended the current user
PFQuery *userIsFriendReceiver = [PFQuery queryWithClassName:#"UserRelationships"];
[userIsFriendReceiver whereKey:#"to" equalTo:[PFUser currentUser]];
[userIsFriendReceiver whereKey:#"active" equalTo:#YES];
[userIsFriendReceiver includeKey:#"from"];
PFQuery *query = [PFQuery orQueryWithSubqueries:#[userIsFriendSender, userIsFriendReceiver]];
Try moving includeKey: clauses to the joint query.
PFQuery *query = [PFQuery orQueryWithSubqueries:#[userIsFriendSender, userIsFriendReceiver]];
[query includeKey:#"from"];
[query includeKey:#"to"];

Resources