IOS Parse如何根据两个类从查询中检索对象?

时间:2014-11-02 17:07:49

标签: ios objective-c parse-platform pfquery pfobject

我有两个班级UserPostUser类有一个userType字段,我想从给定的userType中检索所有帖子,然后将其称为组x。在Post类中,我有一个指向User类的指针。

我试图做类似的事情,首先检索我想要的用户类型的所有用户ID:

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

然后我想基于这些objectIds进行查询,但我不确定如何查询此数组或者这是否是正确的方法。怎么办呢?

1 个答案:

答案 0 :(得分:3)

Parse在查询时提供matchesQuery方法,所以......

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