Facebook api电话 - 共同的朋友 - 循环

时间:2015-07-31 00:55:47

标签: c# loops facebook-graph-api foreach while-loop

我有这个代码调用Facebook API,以便让两个给定用户的应用程序上的共同朋友。参数nextPage对应于facebook api的下一页。 问题是,虽然我每次只有有限数量的普通朋友,但我从Facebook api那里得到错误,因为每秒呼叫次数太多(总体上约为500万/天)。我试图模拟对facebook的调用,总是返回null,但由于w3p导致CPU过载,使用率为99%。我错过了什么?

public async Task<MutualFriendsModel> GetMutualFriends(Guid currentUserGuid,
        Guid visitedUserGuid, string nextPage)
{
    var currentUser = Get(currentUserGuid);
    var visitedUser = _serviceUserLogin.GetByUserId(visitedUserGuid);
    var mutualFriends = new MutualFriendsModel();
    var hasNextPage = true;
    while (hasNextPage && mutualFriends.Users.Count < 25)
    {
        var facebookResult = await
                     _facebookApi.GetMutualFriendsFacebookRequest(currentUser.Token,
                        visitedUser.ProviderKey, nextPage);
        if (facebookResult == null) break;
        mutualFriends.Update(facebookResult, this, _serviceUserLogin);
        nextPage = mutualFriends.NextPageUrl;
        hasNextPage = !string.IsNullOrEmpty(mutualFriends.NextPageUrl);
     }

     return mutualFriends;
}

此外,还有上述片段的另一种变体,它只统计共同的朋友。

private async Task<IList<SavedCommentModel>> MutualFriendsCount(User currentUser,
                        IList<SavedCommentModel> comments)
{
    var usersFriends = new Dictionary<Guid, long>();
    foreach (var comment in comments)
    {
        if (usersFriends.ContainsKey(comment.UserId))
        {
            comment.TotalMutualFriends = usersFriends[comment.UserId];
        }
        else
        {
            var visitedUser = _serviceUserLogin.GetByUserId(comment.UserId);
            if (visitedUser.LoginProvider != LoginProvider.Facebook.ToString()) continue;
            var facebookResult = await
                            _facebookApi.GetMutualFriendsFacebookRequest(currentUser.Token, visitedUser.ProviderKey);
            if (facebookResult == null) continue;
            comment.TotalMutualFriends = facebookResult.Context.Mutual_friends.Summary.Total_Count;
            usersFriends.Add(comment.UserId, comment.TotalMutualFriends);
        }
    }
    return comments;
}

0 个答案:

没有答案