2012-12-20 58 views
0

任何人都知道如何縮小這個,這樣我就不必在QueryForTable方法的主線程中運行查詢了?我使用Parse.com任何人都知道如何在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

如果我讀這正確的,你想爲所有的照片通過的追隨者或者你身邊的查詢。爲什麼不使用:

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個元素,則可能需要使用內部查詢返回的最大元素數量;一路上的每個查詢都受限於其自己的查詢限制。

相關問題