Firebase Asynch programming

Hello everyone

I didn’t know just where to put this topic, so as it is kind of a utility to creating a multiplayer game I decided to post it here.

I am having trouble understanding this code i have here:

public double GetBankBalance()
    {
        double bankBalance = 0;

        Router.PlayerMoney("ymO6Hyl8WjbuPGaWV7FksbVABGb2").GetValueAsync().ContinueWith(task =>
        {
        if (task.IsFaulted)
            {
                Debug.Log("Error in GetBankBalance()");
            }
        else if (task.IsCompleted)
            {
                DataSnapshot snapshot = task.Result;
                bankBalance = Convert.ToDouble(snapshot.Child("bankBalance").Value);
                //UpdateBankBalanceText(this.bankBalance);
            }
        

        });

        return bankBalance;
    }

So I am trying to get a value from my database so I can return it as a value to use when needed. The problem is that the “return bankBalance;” is executed before the task is done. I get the correct value, but too late. I can’t seem to figure out how to wait for the task to be finished. I can’t use Task.Wait(t) because for some reason it keeps throwing that the Wait method isn’t in the definition of “Task” (yes I added a using statement so I can use “Task”).

Could anyone explain how I can make this work? Tried a lot and searched on the internet but I can’t seem to figure it out.

Thanks in advance

Hi.
Please use Code tags.
You should use a Async solution, so you can use Callbacks in this case.
Thanks.

Thanks for the response.
Care to explain how I could implement this best in this method?

Create a delegate and accept the callback:

public delegate BankBalanceCallback (double balance);

public double GetBankBalance(BankBalanceCallback cb)
{
    double bankBalance = 0;
    Router.PlayerMoney("ymO6Hyl8WjbuPGaWV7FksbVABGb2").GetValueAsync().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            Debug.Log("Error in GetBankBalance()");
        }
        else if (task.IsCompleted)
        {
            DataSnapshot snapshot = task.Result;
            bankBalance = Convert.ToDouble(snapshot.Child("bankBalance").Value);
            if (cb != null) {
                cb(bankBalance);
            }
            //UpdateBankBalanceText(this.bankBalance);
        }
    });
    return bankBalance;
}

It is the solution, or you can have a look at the resources below:

Thanks.

Thanks for your help. But I still can’t figure out how to properly implement that into my code so I’ll just try to add it to the task from within the task.

Thanks again for the assistance!

Same question here too.

The callback is not working.

For future googlers who may stumble here like I did:

You will need the return value in the delegate:

public delegate void BankBalanceCallback (double balance);