nwales
nwales

Reputation: 3561

Parse - How to turn two part query into single query?

I have Group PFObjects and Item PFObjects. Each Item contains a pointer to the Group it belongs to. An item can only point to one Group. In my collection view the Item objects are displayed under section headers according to which Group they belong to.

Initially I was building a query that gets all Item objects that belong to a user and from that constructed a dictionary of Item array objects keyed by Group that are pulled from each Item

var query = PFQuery(className: "Item")
query.whereKey("createdBy", equalTo:PFUser.currentUser())
query.includeKey(“Group”)

But, now i have a problem. If a Group has not pointers to it, I would still like to display that Group.

What I really need is to pull all Groups that belong to a user and all Items that point to those Groups. I know this can be accomplished with a pair of queries, but since I am subclassing the parseUI QueryCollectionViewController it would be much cleaner if I could pull this off in a single query. Is it possible?

Upvotes: 0

Views: 55

Answers (2)

Bart de Ruijter
Bart de Ruijter

Reputation: 952

It depends if you know if a group has been created by an user or not. IF a group points to the user who made it, then I think you can accomplish this with one query. I personally develop in android, but I believe functions are practically the same for iOS as for Parse. Either way to the case:

PFQuery *query = [PFQuery queryWithClassName:@"Group"];
[query includeKey:@"Item"];
[query whereKey:@"createdBy" equalTo:PFUser.currentUser()];
[query findObjectsInBackgroundWithBlock:^(NSArray *groups, NSError *error) {
    // retrieves all groups, that have been created by the current user
    // A group contains all the item objects as well thanks to the include key!
}];

BUT, if you want to find all groups based on items created by the user and all the groups that have no items you'd have to use an inner query. The idea is as follows

Get all groups.

except the ones that have items

unless they have items of the current user.

Which would look something like this:

//First inner query
PFQuery *innerQueryItems = [PFQuery queryWithClassName:@"Items"];
[query whereKey:@"createdBy" notEqualTo:@PFUser.currentUser()];

PFQuery *mainQuery = [PFQuery queryWithClassName:@"Group"];
[query whereKey:@"items" doesNotMatchesQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *groups, NSError *error) {
    // retrieves all groups unless they have items that do not belong to the current user.
}];

I'm not sure if the above code works, but I believe this is what you were aiming for!

Some nice reference material

Basic iOS doc from parse, always handy. Link to the filters you can apply on a query! Definitely worth checking out

Upvotes: 0

Dániel Nagy
Dániel Nagy

Reputation: 12015

With one query: you cannot. You need two queries, BUT: If you want a clean solution as you said, use cloud code. With that, you can download anything you want in one "query".

Upvotes: 1

Related Questions