2014-10-29 40 views
1

目前,我正在查詢數據庫兩次以獲得當前用戶尚未跟蹤的所有附近用戶的列表。這裏是我的嵌套查詢:IOS解析是我的嵌套查詢比較對象與指針的一個很好的解決方案嗎?

// List of all users being followed by the current user 
PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:kFTActivityClassKey]; 
[followingActivitiesQuery whereKey:kFTActivityTypeKey equalTo:kFTActivityTypeFollow]; 
[followingActivitiesQuery whereKey:kFTActivityFromUserKey equalTo:[PFUser currentUser]]; 
[followingActivitiesQuery setCachePolicy:kPFCachePolicyNetworkOnly]; 
[followingActivitiesQuery includeKey:kFTActivityToUserKey]; 
[followingActivitiesQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 

     NSMutableArray *followedUserIds = [[NSMutableArray alloc] init]; 

     // Obtain an array of object ids for all users being followed 
     for (PFObject *object in objects) { 
      PFUser *followedUser = [object objectForKey:kFTActivityToUserKey]; 
      [followedUserIds addObject:followedUser.objectId]; 
     } 

     PFGeoPoint *geoPoint = [[PFUser currentUser] objectForKey:kFTUserLocationKey]; 

     // List of all users within 50 miles that are not already being followed 
     PFQuery *followUsersByLocationQuery = [PFQuery queryWithClassName:kFTUserClassKey]; 
     [followUsersByLocationQuery whereKey:kFTUserObjectIdKey notEqualTo:[PFUser currentUser].objectId]; 
     [followUsersByLocationQuery whereKey:kFTUserLocationKey nearGeoPoint:geoPoint withinMiles:50]; 
     [followUsersByLocationQuery whereKeyExists:kFTUserLocationKey]; 
     [followUsersByLocationQuery whereKey:kFTUserObjectIdKey notContainedIn:followedUserIds]; 
     [followUsersByLocationQuery setLimit:100]; 
     [followUsersByLocationQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
      if (!error) { 
       self.objects = objects; 
       [self.tableView reloadData]; 
      } 
     }]; 
    } 
}]; 

我的問題是,這是一個可行的解決方案?爲了達到這個目的,必須兩次查詢服務器,我感到內疚,但我無法在一次查詢中完成所有操作。我無法將ActivityClass的解析指針與Users類進行比較,因爲Users類是指向的類,正因爲如此,我無法想出在一個查詢中完成所有操作的方法。

回答

0

你很幸運 - 這可以在使用PFQuerywhereKey:doesNotMatchKey:inQuery:方法的單個查詢中完成。

解析會將其視爲針對數據庫的單個複合查詢。共內疚免費:)

試試這個:

// List of all users being followed by the current user 
PFQuery *followingActivitiesQuery = [PFQuery queryWithClassName:kFTActivityClassKey]; 
[followingActivitiesQuery whereKey:kFTActivityTypeKey equalTo:kFTActivityTypeFollow]; 
[followingActivitiesQuery whereKey:kFTActivityFromUserKey equalTo:[PFUser currentUser]]; 
[followingActivitiesQuery setCachePolicy:kPFCachePolicyNetworkOnly]; 
[followingActivitiesQuery includeKey:kFTActivityToUserKey]; 

PFGeoPoint *geoPoint = [[PFUser currentUser] objectForKey:kFTUserLocationKey]; 

// List of all users within 50 miles that are not already being followed 
PFQuery *followUsersByLocationQuery = [PFQuery queryWithClassName:kFTUserClassKey]; 
[followUsersByLocationQuery whereKey:kFTUserObjectIdKey notEqualTo:[PFUser currentUser].objectId]; 
[followUsersByLocationQuery whereKey:kFTUserLocationKey nearGeoPoint:geoPoint withinMiles:50]; 
[followUsersByLocationQuery whereKeyExists:kFTUserLocationKey]; 
//The next line is your new compound query 
[followUsersByLocationQuery whereKey:kFTUserObjectIdKey doesNotMatchKey:@"objectId" inQuery:followingActivitiesQuery]; 
[followUsersByLocationQuery setLimit:100]; 
[followUsersByLocationQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 
     self.objects = objects; 
     [self.tableView reloadData]; 
    } 
}]; 
+0

非常感謝你對嘗試這一點。不幸的是,這不起作用,因爲'kFTActivityClassKey'包含一個指向'kFTUserClassKey'的指針,並且使用inQuery,這兩個類只會比較這兩個類的'objectId',而不是比較指針的指針objectId 'kFTActivityClassKey'到'kFTUserClassKey'。它不會使用所指向的類的objectIds。但是,謝謝你,值得一試! – SuperKevin 2014-10-29 13:52:33

+0

呃,drat。 :)祝你好運都一樣! – 2014-10-30 01:06:19

相關問題