question

Kim Strasser avatar image
Kim Strasser asked

What can I do so that my API calls are executed in the correct order?

At the beginning, the player makes his login call with PlayFabClientAPI.LoginWithIOSDeviceIDAsync.

If the API call is successful, then I always want to retrieve the players data. But I'm not sure if PlayerAccountDetails() is only executed after GetPlayFabAccountInfo() has finished. What can I do to ensure that PlayerAccountDetails() is only executed after GetPlayFabAccountInfo() has finished?

Is it possible to change private void GetPlayFabAccountInfo() to private async Task GetPlayFabAccountInfo() ?

In addition, the line PlayerLoginSuccessful = true is executed before PlayerAccountDetails() has finished. I only want that PlayerLoginSuccessful = true when PlayerAccountDetails() has finished. What can I do to ensure that the line PlayerLoginSuccessful = true is executed only after PlayerAccountDetails() has finished?

string PlayerPlayFabID = "";
string EntityID = "", EntityType = "";
string PlayersessionTicket = "";
bool PlayerLoginSuccessful = false;

private void RegisterGuestPlayFabAccount()
{
    PlayerDeviceId = ReturnMobileID();
    var requestIOS = new LoginWithIOSDeviceIDRequest { DeviceId = PlayerDeviceId, CreateAccount = true };
    var loginTask = PlayFabClientAPI.LoginWithIOSDeviceIDAsync(requestIOS);
    loginTask.ContinueWith(OnPlayFabRegisterGuestAccountComplete);
}

private void OnPlayFabRegisterGuestAccountComplete(Task<PlayFabResult<LoginResult>> task)
{
    var newLabel = "Unknown failure";
    if (task.Result.Result != null)
    {
        newLabel = "Congratulations, you made your first successful API call!";
        EntityID = task.Result.Result.EntityToken.Entity.Id;
        EntityType = task.Result.Result.EntityToken.Entity.Type;
        PlayersessionTicket = task.Result.Result.SessionTicket;
        GetPlayFabAccountInfo();
        PlayerAccountDetails();
        PlayerLoginSuccessful = true;
    }

    if (task.Result.Error != null)
    {
        OnPlayFabError(task.Result.Error);
    }
}

private void GetPlayFabAccountInfo()
{
    var request = new GetAccountInfoRequest { };
    var loginTask = PlayFabClientAPI.GetAccountInfoAsync(request);
    loginTask.ContinueWith(OnGetPlayFabAccountInfoComplete);
}

private void OnGetPlayFabAccountInfoComplete(Task<PlayFabResult<GetAccountInfoResult>> task)
{
    var newLabel = "Unknown failure";
    if (task.Result.Result != null)
    {
        newLabel = "Congratulations, you made your first successful API call!";
        PlayerPlayFabID = task.Result.Result.AccountInfo.PlayFabId;
    }

    if (task.Result.Error != null)
    {
        newLabel = "Something went wrong with your first API call.\n"
                    + "Here's some debug information:\n"
                    + task.Result.Error.GenerateErrorReport();
    }
}

public async void PlayerAccountDetails()
{
    await GetPlayerCountryData();
    await GetPlayerTagsData();
}

private async Task GetPlayerTagsData()
{
    var resultprofile = await PlayFabServerAPI.GetPlayerTagsAsync(new PlayFab.ServerModels.GetPlayerTagsRequest()
    {
        PlayFabId = PlayerPlayFabID
    });

    if (resultprofile.Error != null)
        Console.WriteLine(resultprofile.Error.GenerateErrorReport());
    else
    {
        if ((resultprofile.Result != null) && (resultprofile.Result.Tags.Count() > 0))
            Resultat = resultprofile.Result.Tags[0].ToString();
    }
}

private async Task GetPlayerCountryData()
{
    var resultprofile = await PlayFabClientAPI.GetUserDataAsync(new PlayFab.ClientModels.GetUserDataRequest()
    {
        PlayFabId = PlayerPlayFabID,
        Keys = null
    });

    if (resultprofile.Error != null)
        Console.WriteLine(resultprofile.Error.GenerateErrorReport());
    else
    {
        if (resultprofile.Result.Data == null || !resultprofile.Result.Data.ContainsKey("Country") || !resultprofile.Result.Data.ContainsKey("City"))
            Console.WriteLine("No Country/City");
        else
        {
            Console.WriteLine("Country: " + resultprofile.Result.Data["Country"].Value);
            Console.WriteLine("City: " + resultprofile.Result.Data["City"].Value);
        }
    }
}
Player Data
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
JayZuo avatar image
JayZuo answered

To achieve what you want, you can remove

 PlayerAccountDetails();
 PlayerLoginSuccessful = true;

from OnPlayFabRegisterGuestAccountComplete method.

Then add PlayerAccountDetails(); into OnGetPlayFabAccountInfoComplete method like

    if (task.Result.Result != null)
    {
        newLabel = "Congratulations, you made your first successful API call!";
        PlayerPlayFabID = task.Result.Result.AccountInfo.PlayFabId;
            PlayerAccountDetails();
    }

And add into

public async void PlayerAccountDetails()
{
    await GetPlayerCountryData();
    await GetPlayerTagsData();
    PlayerLoginSuccessful = true;
}

Besides, I'd suggest reading Asynchronous programming for how to write asynchronous code.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.