Ad

Problem In Retrieve Data From Firebase Database In C#

I'm currently trying to retrieve data from my Firebase database. The current function I able only show the data. However after I retrieve the data I unable to pass to other variable or pass into a constructor.

Function

public Ally ReadCard() // 
{
    Ally ally = new Ally();[enter image description here][1]
    FirebaseDatabase.DefaultInstance.GetReference("Cards").GetValueAsync().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {

        }
        else if (task.IsCompleted)
        {

        }

        DataSnapshot dataSnapshot = task.Result;
        var json = dataSnapshot.Child("P001-0000-0001").GetRawJsonValue();
        AllyBean card = JsonUtility.FromJson<AllyBean>(json);

        Debug.Log("Card Description : " + card.CardDescription);
        Debug.Log("Card Name : " + card.CardName);
        Debug.Log("Card Type : " + card.CardType);
        Debug.Log("Card Atk : " + card.AllyBeanProperties.Atk);
        Debug.Log("Card Hp : " + card.AllyBeanProperties.Hp);
        ally = new Ally(card.CardName, card.CardDescription, card.CardType, card.AllyBeanProperties.Atk, card.AllyBeanProperties.Hp); 

    });
    return ally;
}

The purpose of this function is to allow me retrieve the data and cast it into a class object.

Main Program

 void Start()
{
    databaseUtils = new DatabaseUtils();
    Debug.Log( "Main value : " + databaseUtils.ReadCard().CardName); //trying to retrieve data from database
}

Problem

However base on the console, the data is only shown in Debug.Log() only. The data does not appear been inserted into the constructor.

    Debug.Log("Card Description : " + card.CardDescription); 
    Debug.Log("Card Name : " + card.CardName);
    Debug.Log("Card Type : " + card.CardType);
    Debug.Log("Card Atk : " + card.AllyBeanProperties.Atk);
    Debug.Log("Card Hp : " + card.AllyBeanProperties.Hp);
    ally = new Ally(card.CardName, card.CardDescription, card.CardType, card.AllyBeanProperties.Atk, card.AllyBeanProperties.Hp);  // new ally object is empty

Debug Result

Firebase Structure

Any help is welcome. Thank you

Ad

Answer

Changes

Main file

DatabaseUtils databaseUtils;

async void Start()
{
    databaseUtils = new DatabaseUtils();
    Ally ally = new Ally("card name 12", "card description 12", CardType.Ally, 20, 30);
    databaseUtils.AddCard(ally);
    await databaseUtils.ReadCard();

    Debug.Log(databaseUtils.GetCard().CardName);
}

Read function

public async Task ReadCard()
{
    await FirebaseDatabase.DefaultInstance.GetReference("Cards").GetValueAsync().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {

        }
        else if (task.IsCompleted)
        {
            DataSnapshot dataSnapshot = task.Result;
            var json = dataSnapshot.Child("P001-0000-0001").GetRawJsonValue();
            Ally card = JsonUtility.FromJson<Ally>(json);

            Debug.Log(card.CardDescription);
            Debug.Log(card.CardName);
            Debug.Log(card.CardType);

            Ally ally = new Ally(card.CardName, card.CardDescription, card.CardType, card.Atk, card.Hp);
            SetCard(ally);
        }

    });
}

public void SetCard(Card card)
{
    this.card = card;
}

public Card GetCard()
{
    return card;
}

Note

I receive 'async function' is not available in C# 4' error code. I update my VS and used donet4 instead

Ad
source: stackoverflow.com
Ad