Search Unity

[Facebook Unity SDK] Issues while storing multiple FB API calls results in array.

Discussion in 'Editor & General Support' started by mickee, Jul 7, 2017.

  1. mickee

    mickee

    Joined:
    Dec 26, 2013
    Posts:
    7
    Hello,

    I am trying to retrieve 25 non-friends facebook users first names from Facebook but I have issues while using indexes in callback :

    Code (CSharp):
    1.  
    2. m_leaderboard = new LeaderboardData[25];
    3. for (int i = 0; i < 25; i++)
    4. {
    5.           yield return new WaitForSeconds(0.5f);
    6.  
    7.           // We just have to load first names
    8.           FB.API("/" + m_leaderboard[i].fuid + "?fields=first_name", Facebook.Unity.HttpMethod.GET,
    9.               delegate (IGraphResult result)
    10.               {
    11.                   if (result.Error != null) return;
    12.                   Debug.LogWarning("Index { " + i + " }");
    13.                   m_leaderboard[i].first_name = result.ResultDictionary["first_name"] as String;        
    14.               }                                  
    15.           );
    where m_leaderboard is just a struct tab storing leaderboard infos :
    Code (CSharp):
    1.  
    2. public struct LeaderboardData
    3.        {
    4.                public long fuid;
    5.                public string first_name;
    6.                public Texture image;
    7.                public int score;
    8.                public int rank;
    9.        };
    10.  
    11.        public LeaderboardData[] m_leaderboard;
    12.  
    The problem is indexes are wrong in callback (timing issues, some "i" are skipped), how can I chain several API calls and store results in array ? (I could probably do 25 API calls and index them without the for loop but ...).

    Second problem is how can I trigger an event when all callback threads are terminated :
    Actually, I work with a counter incremented in callback which fires an event when 25 values are received but it seems to me that it's not optimal. I was thinking of something like Thread.Join() in C and fire event when all callbacks are finished.

    Thanks for your help,
    mickee