IOS Parse是我的嵌套查询,是比较对象和指针的好方法吗?

时间:2014-10-29 04:27:42

标签: ios objective-c parse-platform pfquery

目前,我正在两次查询数据库,以获得当前用户尚未关注的所有附近用户的列表。这是我的嵌套查询:

// 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类是指向的类,因此我无法想到在一个查询中完成所有操作的方法。

1 个答案:

答案 0 :(得分:0)

您很幸运 - 可以使用whereKey:doesNotMatchKey:inQuery:的{​​{1}}方法在单个查询中完成此操作。

Parse会将此视为针对数据库的单个复合查询。完全没有内疚:)

请改为尝试:

PFQuery