0
votes

I'm using Parse as my backend and I'm currently looking for a better way for my situation.
I'll use Objective-C in order to demonstrate but I didn't tag it as Objective-C because it's not language related.
My situation is as follows: I have a class let's call it X. that has a key let's call it Y which is PFRelation of type Z (Y<Z>). I need to get a list of Z. like to query for Z (Z is all I'm interested in). Moreover, if possible, I want to be able to limit the output of Z like PFQuery does.
To sum up: I need a PFQuery that will be able to do all the abilities of PFQuery e.g. findObjectsInBackgroundWithBlock: and countObjectsInBackgroundWithBlock that the object I will be operating in, in the block will be of NSArray of type Z.

My current solution: I thought of querying for X where exists Y (so I won't get empty objects of X. and just for every X, load all Y's and go through all arrays of Z.

I really don't think my solution is the best, hoping for someone that can point me at another solution to fit my situation.

1
I'm having problems grasping what it is you're after. Could you try explaining with a more mundane example objects (like cars, parts, books, authors, albums, artists etc) instead of XYZ? Maybe it's just too early in the morning for me, but... oh well :-) - Marius Waldal
Let's say X is a car workshop and Y is a PFRelation of cars and Z is the type Car. I need a query to go through so I'll have only cars that match my query on the workshops. I think this should do... Once again early in the morning. Say if it's not good enough - user2558461
Sooo, you want a list of cars, but only cars that... what? Only cars from a specific list of workshops? If the workshop meets a criteria, give me the cars they have? Can you provide some code you are already trying, or maybe some pseudo code? Or just provide the exact use case :-) - Marius Waldal
I want the list of cars that doesn't have any criteria. Its parent class, workshops meet a criteria. Like all cars that their workshop is in the usa. I don't have a psuedo... Because I'm seriously out of luck with how to make it efficient - user2558461
If you still don't understand, please tell me , I'll update my question - user2558461

1 Answers

2
votes

I think might be what you are looking for:

PFQuery *wsQuery = [PFQuery queryWithClassName:@"Workshop"];
[wsQuery whereKey:@"country" equalTo:@"USA"];  // or whatever your criterias
NSArray *workshops = [wsQuery findObjects];

for (PFObject *workshop in workshops) {
    PFQuery *carsQuery = [workshop relationforKey:@"cars"].query;
    [carsQuery setLimit:5];
    // Add whatever constraints you want here
    NSArray *wsCars = [carsQuery findObjects];
    for (PFObject *car in wsCars) {
        NSLog(@"Car make: %@", [car objectForKey:@"make"]);
    }
}

This will first get a list of all workshops, and then for each workshop will get a list of all the cars belonging to that workshop, and print the make (brand) for each car.

UPDATE

New suggestion based on comments:

PFQuery *wsQuery = [PFQuery queryWithClassName:@"Workshop"];
[wsQuery whereKey:@"country" equalTo:@"USA"];

PFQuery *carQuery = [PFQuery queryWithClassName:@"Car"];
[carQuery whereKey:@"workshop" matchesQuery:wsQuery];
[carQuery setLimit:5];
[carQuery findObjectsInBackgroundWithBlock:^(NSArray *cars, NSError *error) {
    // cars contains only cars with workshops located in USA 
}];