Create PFQuery for PFRelation with zero objects - ios

I want to create a PFQuery for a PFRelation to find objects where the specified relation has zero objects in it. Here's what I've tried:
PFQuery *categoryQuery = [CatalogCategory query];
[categoryQuery whereKeyDoesNotExist:#"subcategories"];
But I get an error saying I can't use this operator on a PFRelation key. How else could I achieve what I'm looking for?

Have you tried [categoryQuery whereKey:#"subcategories" equalTo:nil]?
It may be that the whereKeyDoesNotExist: method is unavailable for objects of the PFRelation type.

Related

How do I fetch all relation objects in Parse.com IOS?

I have the following query:
PFObject *photoData = [PFObject objectWithClassName:#"Photos"];
PFRelation *relation = [photoData relationForKey:#"photo"];
PFQuery *query = [PFQuery queryWithClassName:#"People"];
query = [relation query];
[query whereKey:#"deleted" equalTo:#NO];
[query whereKey:#"createdAt" lessThan:_createdAt];
[query orderByDescending:#"createdAt"];
query.limit = 20;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
...
}
I have a database table in Parse called People. In that table, there is a bunch of data but has a relation called photo. Now, I am saving one photo (with its data) in the photo relation. In the Parse dashboard, the data is saved correctly.
How do I fetch that back? Right now I have constraints on this system (and question) that each People objects has ONLY one photo object. So I need to fetch it all back at once.
Relation is just what its literal meaning is. It does not contain any data. If you want to query the data, you need to get the PFQuery from PFRelation by query method like so:
PFRelation *relation = [photoData relationForKey:#"photo"];
PFQuery *photoQuery = [relation query];
// perform your photoQuery here
If you limit them by one object only, then you can change your photo as Pointer type instead of using Relation.

parse web service relational database

I am using parse to develop an iOS app.
For the database, I have three tables, named user, UserAndCourse, and course.
Where UserAndCar stores a pointer to user and course.
the tables looks like:
My problem is:
How can I query the courses that belongs to the current user on iOS. i.e that look into UserAndCourse to find the rows that user column is current user and query for the course object that is pointed to by the course column.
Can I do this in a single relational query? or I have to query the UserAndCourse table rows first and then query the course it pointed to.
I suggest to use PFRelation, that way you don't need to create new table for relations like userAndCourse. Parse automatically will link through the relation. You just need to create relation between them and do what you want.
Saving relation;
PFUser *user = [PFUser currentUser];
PFRelation *relation = [user relationForKey:#"course"];
[relation addObject:coursePFObject];
[user saveInBackground];
And query when you need to reach them;
PFRelation *relation = [user relationForKey:#"course"];
PFQuery *query = [relation query];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
// results contains all the people who subscribe to course
}];
Moreover you can specify your course by adding more queries on it;
PFQuery *query = [relation query];
[query whereKey:#"name" equalTo:#"Math"];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
// results contains all the people who subscribe Math course
}];
I think it's better way to shape your tables in parse. Otherwise you need to create table for each relation.
Hope it helps also if you need more detail this can be helpful.

Adding constraints to a PFQuery with objects from a PFRelation

I am using a PFQueryTableViewController and I am querying PFObjects called Group. These groups objects all have relations to a User PFObject. I want to add constraints to this query by querying only the groups that have relations containing the current user. How do I add constraints to a query by only returning objects that contain the current user in the relation?
-(PFQuery *)queryForTable{
PFQuery * query = [PFQuery queryWithClassName:#"Group"];
//Users is a relation
[query whereKey:#"Users" equalTo:[PFUser currentUser]];
//This crashes the app
//I need to return only objects with current user in the users relation
return query;
}
Thanks

How to do a dynamic query based on an NSArray of data with parse.com on iOS

I need to query a large number of friends messages for a user in Parse.
I have a Friend_Relation object with two users, a friend and a current user.
Each user has many Message objects (i am concerned only with the most current). The relation loos like this:
[message setObject:currentUser forKey:#"userMessage"];
The user object has an attribute for the most current message objectId.
How do I get all of a user's friends and their messages?
I was thinking if I could query all of a user's friends that is easy. If I could then create an NSArray of the objectIds that I get from a users friends, I could another single query to get back all of those user's messages. How could I create this type of query on the fly?
You can probably use an inner query to achieve this:
PFQuery *getAllFriendsInnerQuery = [[currentUser relationForKey:#"friend"] query];
PFQuery *messageQuery = [PFQuery queryWithClassName:#"Message"];
[messageQuery whereKey:#"userMessage" matchesQuery:getAllFriendsInnerQuery];
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
// Here the parameter array contains all the messages from current user's friends
}];
The answer is definitely to use the contained in method like so:
NSArray *names = [self.user objectForKey:#"friends"];
[query whereKey:#"userMessaga" containedIn:names];

query for pfobjects with pffile properties fails to return values with parse.com

I am doing a query based on the contents of an nsarray like this:
PFQuery *query = [PFQuery queryWithClassName:#"Message"];
NSArray *friends = [self.user objectForKey:#"friends"];
[query whereKey:#"userMessage" containedIn:friends];
[query findObjectsInBackground];
The Message PFobjects have an PFFile (image) for each Message. The query does not return any Messages with PFFile images. If a message does not have a PFFile associated with it, the query returns the Message with no problems.
Why am I not able to receive results for my query for those messages with PFFiles associated with them?
Edit:
I tried to not use a "containedIn" query and I was able to return results with this:
PFQuery *query = [PFQuery queryWithClassName:AWAY_MESSAGE];
[query whereKey:#"userMessage" equalTo:[PFUser currentUser]];
This doesn't solve my problem of getting all of the Messages for a user's friends, but it does show there is a bug with the using the containedIn query.
Is this in fact a bug, or is there a problem with my first query?
The problem was that the files I was querying had ACLs on them.

Resources