任何人都知道如何在QueryForTable中缩小此查询(使用Parse.com)

时间:2012-12-20 10:14:49

标签: ios parse-platform

任何人都知道如何缩小它,以便我不必在QueryForTable方法的主线程中运行查询?我正在使用Parse.com

//Find who are the users following (RELATIONSHIP OBJECT)
PFQuery *followeesQuery = [PFQuery queryWithClassName:@"Relationship"];
[followeesQuery whereKey:@"Follower" equalTo:[PFUser currentUser]];
NSArray *followees = [followeesQuery findObjects];
//Filter followees
self.followees = [[NSMutableArray alloc] initWithCapacity:[followees count]];
for (int i = 0; i < [followees count]; i++) {
    PFObject *followee = followees[i];
    PFUser *user = (PFUser *)[followee objectForKey:@"User"]; //USER OBJECT
    [self.followees addObject:user];
}


PFQuery *nearbyPhotosQuery = [PFQuery queryWithClassName:@"Photo"]; //PHOTO OBJECT
[nearbyPhotosQuery whereKey:@"User" notContainedIn:self.followees];
[nearbyPhotosQuery whereKey:@"Location" nearGeoPoint:self.userCurrentLocation withinKilometers:10];

PFQuery *followersPhotoQuery = [PFQuery queryWithClassName:@"Photo"]; //PHOTO OBJECT
[followersPhotoQuery whereKey:@"User" containedIn:self.followees];

NSMutableSet *set = [NSMutableSet setWithArray:[nearbyPhotosQuery findObjects]];
[set addObjectsFromArray:[followersPhotoQuery findObjects]];

NSArray *targetPhotoObjects = [set allObjects];

NSMutableArray *targetIDs = [[NSMutableArray alloc] initWithCapacity:[targetPhotoObjects count]];
for (int i = 0; i < [targetPhotoObjects count] ; i++) {
    PFObject *photoObject = targetPhotoObjects[i];
    [targetIDs addObject:photoObject.objectId];
}


PFQuery *targetPhotoQuery = [PFQuery queryWithClassName:@"Photo"];
[targetPhotoQuery whereKey:@"objectId" containedIn:targetIDs];


return targetPhotoQuery;

1 个答案:

答案 0 :(得分:1)

如果我读得正确,您想要追随者或您附近的所有照片的查询。为什么不使用:

PFQuery *followeesQuery = [PFQuery queryWithClassName:@"Relationship"];
[followeesQuery whereKey:@"Follower" equalTo:PFUser.currentUser];

PFQuery *followeesPhotosQuery = [PFQuery queryWithClassName:@"Photo"];
[followeesPhotosQuery whereKey:@"User" matchesKey:@"User" inQuery:followeesQuery];

PFQuery *nearbyPhotosQuery = [PFQuery queryWithClassName:@"Photo"];
[nearbyPhotosQuery whereKey:@"Location"
               nearGeoPoint:self.userCurrentLocation
            withinKilometers:10];

return [PFQuery orQueryWithSubqueries:@[followeesPhotosQuery, nearbyPhotosQuery]];

这是一个匹配您正在查找的查询,不需要服务器端创建。如果您希望在流程中的任何步骤中返回超过100个元素,则可能需要使用内部查询返回的最大元素数;沿途的每个查询都受其自己的查询限制。