4. Nhận dữ liệu

  • Sử dụng GetSnapshotAsync(): để nhận dữ liệu

Code mẫu:

void Start()
{   
    LoadFireStore("Main", firebaseUser.UserId);
}
private void LoadFireStore(string collectionName, string documentId)
{
    FirebaseFirestore.DefaultInstance.Collection(collectionName).Document(documentId).GetSnapshotAsync().ContinueWithOnMainThread(task =>
    {
        if (task.IsFaulted)
        {

            Debug.LogError("Can't connect to database: " + task.Exception);
        }
        else if (task.IsCompleted)
        {
            DocumentSnapshot snapshot = task.Result;
            if (snapshot.Exists)
            {
                try
                {
                    var data = snapshot.GetValue<string>("data");
                    Debug.Log($"Data: {data}");
                }
                catch (System.Exception ex)
                {
                    Debug.LogException(ex);
                }
            }
            else
            {
                Debug.LogError("Khong co du lieu");
            }
        }
    });
}

Kết quả:

Last updated