使用Parse优化Mutual Friends的查询

时间:2014-11-15 11:33:40

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

目前,我正在尝试优化我的getMutualFriends方法。当我打开我的朋友'查看控制器,我为用户当前拥有的每个朋友执行getMutualFriends方法...这不是最优的...但是最简单的解决方案......

继承人的所作所为:

[CBUtility queryForFriends:[PFUser currentUser] block:^(NSArray *friends, NSError *error) {
    [self.friendsActivityIndicator stopAnimating];
    if (error) {
        [[[UIAlertView alloc] initWithTitle:@"Error"
                                    message:[error localizedDescription]
                                   delegate:nil cancelButtonTitle:@"Okay"
                          otherButtonTitles:nil]
         show];
        return;
    }

    if ([friends count] == 0 || !friends) {
        [self.friendsTable addSubview:self.friendsEmptyView];
        return;
    }

    self.friends = [NSMutableArray arrayWithArray:friends];

    [self.friendsTable reloadData];
    [self.friendsEmptyView removeFromSuperview];
    int i = 0;

    //
    // THIS IS THE PART THAT SUCKS!!!
    //
    for (PFObject * friendObject in self.friends) {
        [CBUtility queryForMutualFriends:[friendObject objectForKey:kCBFriendToUserKey] block:^(int mutualFriends, NSError *error) {
            if (error) {
                [[[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil] show];
                return;
            }

            CBFriendsCell *cell = (CBFriendsCell *)[self.friendsTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            [cell setMutualFriends:mutualFriends];

        }];
        i++;
    }
}];

继承人+(void)queryForMutualFriends的样子:

+ (void)queryForMutualFriends:(PFUser *)user block:(void (^)(int number, NSError *error))completionBlock
{

    PFQuery *usersFriends = [PFQuery queryWithClassName:kCBFriendClassKey];
    [usersFriends whereKey:kCBFriendFromUserKey equalTo:user];
    [usersFriends whereKey:kCBFriendStatusKey equalTo:kCBFriendStatusFriendKey];

    PFQuery *currentUsersFriends = [PFQuery queryWithClassName:kCBFriendClassKey];
    [currentUsersFriends whereKey:kCBFriendFromUserKey equalTo:[PFUser currentUser]];
    [currentUsersFriends whereKey:kCBFriendStatusKey equalTo:kCBFriendStatusFriendKey];
    [currentUsersFriends whereKey:kCBFriendToUserKey matchesKey:kCBFriendToUserKey inQuery:usersFriends];
    [currentUsersFriends countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
        if (!error) {
            completionBlock(number, error);
            return;
        }
        completionBlock(-1, error);
    }];

}

因此,我不是运行循环并将单个PFUser对象传递给getMutualFriends方法,而是希望将一组朋友传递给方法并返回一个字符对象数组,其键是' user&# 39;和'计算'及其各自的值(例如@[@{@"user":somePFUser, @"count":5}, @{@"user":anotherPFUser, @"count":20}];

我的意思是,目前此功能正常,但却占用了太多的API请求......

任何人都有关于如何设置PFQuery的想法?

修改

Here was a link to a SQL query that solves the same problem

1 个答案:

答案 0 :(得分:0)

显然,你不能......但你可以限制查询服务器的次数,而不是像我一样检索共同的朋友时查询共同的朋友,而是将结果缓存到内存中。

我在设置单元格属性时通过在cellForIndexPath中进行查询来解决此问题。当加载单元格时,我首先检查缓存以查看是否已经进行查询,如果有,那么我得到缓存数据......如果没有,那么我向服务器查询...只发出问题我看到它没有更新...我想我可以每分钟左右清除缓存,因此用户会自动更新,而不是按下重新加载按钮。