0

目前,我正在尝试优化我的 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":somePFUser, @"count":5}, @{@"user":anotherPFUser, @"count":20}];

我的意思是,这目前工作正常,但占用了太多的 API 请求......

有人对如何设置 PFQuery 有任何想法吗?

编辑:

这是解决相同问题的 SQL 查询的链接

4

1 回答 1

0

No apparently, you cannot... But you can limit the amount of times you query to the server by instead of querying for mutual friends when you retrieve the mutual friends like I did, you instead cache the results into memory...

I solved this issue by making the query in cellForIndexPath when setting a cells attributes. When the cell is loaded, I check cache first to see if the query has already been made, if it has then I get the cache data... If it hasn't then I make a query to the servers... Only issue I see is that it doesn't update... I figure I can clear cache every minute or so, so the user gets updated automatically instead of pressing a reload button.

于 2015-01-23T22:25:49.940 回答