在C#

时间:2019-08-11 13:44:41

标签: c# firebase unity3d firebase-realtime-database

我目前正在尝试从Firebase数据库中检索数据。当前功能我只能显示数据。但是,在检索数据之后,我无法传递给其他变量或传递给构造函数。

功能

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;
}

此功能的目的是允许我检索数据并将其转换为类对象。

主程序

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

问题

但是,基于控制台,数据仅显示在Debug.Log()中。数据似乎没有插入到构造函数中。

    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

欢迎任何帮助。谢谢

2 个答案:

答案 0 :(得分:0)

更改

主文件

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);
}

读取功能

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;
}

注意

我收到的“异步功能”在C#4'错误代码中不可用。我更新了VS,并改用了donet4

答案 1 :(得分:0)

我是该领域的新手,但我希望我的回答对这个问题有所帮助。 :-)

  • 以下代码段提供了从Firebase实时数据库下载数据并将其分配给不同变量并在​​控制台中显示它们的最简单方法。我认为这可能会给您您想要的东西。

注意:-当我们处理API通信类任务时,最好坚持使用异步编程。

  1. 考虑一下您的数据是如何在Firebase中形成的。

Screen shot of db

  1. 根据数据库创建模型类。 (这可用于上传和下载过程)

    internal class StudentDetails
     {
         public string id { get; set; }
         public string Name { get; set; }
         public string Age { get; set; }
         public string Address { get; set; }
     }
    
  2. 创建一个异步方法来下载数据并执行所需的操作。

    public class Demo                                                   
     {                                                                   
     IFirebaseClient firebaseclient2;
     int id, age = 0;
     string name, address = "";                                                        
    
      public async void DownloadStudentDetails()
     {
         FirebaseResponse firebaseResponse = await firebaseclient2.GetTaskAsync("Student");
         StudentDetails student = firebaseResponse.ResultAs<StudentDetails>();
         id = Convert.ToInt32(student.id.Trim());
         name = student.Name.Trim();
         address = student.Address.Trim();
         age = Convert.ToInt32(student.Age.Trim());
         Console.WriteLine(id);
         Console.WriteLine(name);
         Console.WriteLine(age);
         Console.WriteLine(address);
     }                                                                                 
    

    }

相关问题