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
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
Related Questions
- → How to Fire Resize event after all images resize
- → JavaScript in MVC 5 not being read?
- → URL routing requires /Home/Page?page=1 instead of /Home/Page/1
- → Getting right encoding from HTTPContext
- → How to create a site map using DNN and C#
- → I want integrate shopify into my mvc 4 c# application
- → Bootstrap Nav Collapse via Data Attributes Not Working
- → Shopify api updating variants returned error
- → Get last n quarters in JavaScript
- → ASP.NET C# SEO for each product on detail page on my ECOMMERCE site
- → SEO Meta Tags From Behind Code - C#
- → onchange display GridView record if exist from database using Javascript
- → How to implement search with two terms for a collection?
Ad