IOS Parse how do I retrieving objects from a query based on two classes?

谁都会走 提交于 2019-12-22 00:24:41

问题


I have two classes User and Post. The User class has a userType field and I want to retrieve all of the posts from a given userType lets call them group x. In the Post class I have a pointer to the User class.

I was trying to do something like, first retrieve all user Ids for the type of user I want:

PFQuery *queryUser = [PFQuery queryWithClassName:kFTUserClassKey];
[queryUser whereKey:kFTUserTypeKey equalTo:kFTUserTypeX];
[queryUser whereKey:kFTUserLocationKey nearGeoPoint:nearGeoPoint withinMiles:miles];
[queryUser findObjectsInBackgroundWithBlock:^(NSArray *usersTypeX, NSError *error) {
            if (!error) {
                NSMutableArray *objectIds = [[NSMutableArray alloc] init];

                // Add ambassador ids into query
                for (PFObject *userX in usersTypeX) {
                    [objectIds addObject:[PFObject objectWithoutDataWithClassName:kFTUserClassName objectId: userX.objectId]];
                }
            }
}];

And then I wanted to query based on these objectIds but I am not sure how to query on this array or if this is even the correct way to do this. How can this be done?


回答1:


Parse provides a matchesQuery method on query, so ...

PFQuery *innerQuery = [PFQuery queryWithClassName:@"User"];
[innerQuery whereKey:@"userType" equalTo:@"X"];  // fix with your real user type
PFQuery *query = [PFQuery queryWithClassName:@"Post"];
[query whereKey:@"user" matchesQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *posts, NSError *error) {
    // posts are posts where post.user.userType == X
}];


来源:https://stackoverflow.com/questions/26701999/ios-parse-how-do-i-retrieving-objects-from-a-query-based-on-two-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!