I am trying to retrieve data from a table in firebase Realtime database and sort it in a table I have made inside unity. this is how my database structure look like
and this is what my code looks like
using Firebase;
using Firebase.Database;
using System;
using UnityEngine;
using UnityEngine.UI;
using Firebase.Unity;
public class ScheduleRetriever : MonoBehaviour
{
// Table GameObject
public GameObject table;
// Prefab for table rows
public GameObject rowPrefab;
private DatabaseReference mDatabaseRef;
private void Start()
{
// Initialize Firebase
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
var dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available)
{
// Get reference to the "timetable" node
mDatabaseRef = FirebaseDatabase.DefaultInstance.GetReference("timetable");
// Retrieve data from the "timetable" node
mDatabaseRef.GetValueAsync().ContinueWith(task => {
if (task.IsCompleted && !task.IsCanceled && !task.IsFaulted)
{
DataSnapshot snapshot = task.Result;
// Iterate through the children of the snapshot
foreach (var idChild in snapshot.Children)
{
DataSnapshot idSnapshot = idChild.Value as DataSnapshot;
//DataSnapshot idSnapshot = idChild.Value as DataSnapshot;
Debug.LogError("wut ");
// Add the data to the table
AddDataToTable(idChild);
}
}
else
{
Debug.LogError("Data retrieval failed: " task.Exception);
}
});
}
else
{
Debug.LogError(
"Could not resolve all Firebase dependencies: " dependencyStatus);
}
});
}
private void AddDataToTable(DataSnapshot idChild)
{
// Create a new row
GameObject newRow = Instantiate(rowPrefab);
// Set the parent of the new row to the table
newRow.transform.SetParent(table.transform, false);
// Get the text components of the new row
Text[] rowTexts = newRow.GetComponentsInChildren<Text>();
// Set the text of the first column to the "fname" value retrieved from Firebase
rowTexts[0].text = idChild.Child("fname").Value.ToString();
// Set the text of the second column to the "monday" value retrieved from Firebase
rowTexts[1].text = idChild.Child("monday").Value.ToString();
// Set the text of the third column to the "tuesday" value retrieved from Firebase
rowTexts[2].text = idChild.Child("tuesday").Value.ToString();
// Set the text of the fourth column to the "wednesday" value retrieved from Firebase
rowTexts[3].text = idChild.Child("wednesday").Value.ToString();
// Set the text of the fifth column to the "thursday" value retrieved from Firebase
rowTexts[4].text = idChild.Child("thursday").Value.ToString();
// Set the text of the sixth column to the "friday" value retrieved from Firebase
rowTexts[5].text = idChild.Child("friday").Value.ToString();
}
}
can anyone help check my code on why the table in unity wouldn't be filled when I run the scene. and did I code the reference to each node correctly. thank you!
CodePudding user response:
Replace ContinueWith
by ContinueWithOnMainThread
Most of Unity API isn't thread save and can only be used by the Unity main UI thread => you will either get a warning/error in the console or it might get eaten by the task.
As the name says ContinueWithOnMainThread
makes sure you are on the Unity main thread when instantiating, setting the texts etc
besides that hit CTRL S it seems you have unsaved changes there (= yellow lines) ^^