[英]Unity: Do I have to learn one, or both (C#, UnityScript) [closed]
[英]Unity C# Firebase: What do I do if "task.Exception.Message" returns "one or more errors have occurred" and is not returning data?
這是我第一次將 Firebase 與 Unity 一起使用。 我的代碼似乎已連接到我的 Firestore 數據庫,但沒有檢索數據。 我也收到此錯誤消息: System.Threading.Tasks.ContinuationResultTaskFromResultTask 2[Firebase.Firestore.DocumentSnapshotProxy,Firebase.Firestore.DocumentSnapshot] UnityEngine.Debug:Log(Object) <>c:<savedata>b__5_0(Task
1) (at Assets/Scripts/CloudFirebase.cs:60) CloudFirebase.cs:60 對應的行是Debug.Log(task);
並且代碼通常卡在DocumentSnapshot snapshot = task.Result;
行。 System.Threading._ThreadPoolWaitCallback:PerformWaitCallback()
注意:我試圖四處尋找與我的問題相關的答案,但似乎找不到任何答案。 如果您有任何與 Unity 相關的鏈接,請分享。 下面是我的代碼:
FirebaseFirestore db;
Dictionary<string, object> user;
CollectionReference stuRef;
private bool istrue = true;
// Start is called before the first frame update
void Start()
{
Debug.Log("Start called");
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
var dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available)
{
Debug.Log("available for use");
// Create and hold a reference to your FirebaseApp,
// where app is a Firebase.FirebaseApp property of your application class.
//app = Firebase.FirebaseApp.DefaultInstance;
db = FirebaseFirestore.DefaultInstance;
Debug.Log(db.Collection("Users").Document("hello"));
stuRef = db.Collection("Users");
savedata();
// Set a flag here to indicate whether Firebase is ready to use by your app.
}
else
{
UnityEngine.Debug.LogError(System.String.Format(
"Could not resolve all Firebase dependencies: {0}", dependencyStatus));
// Firebase Unity SDK is not safe to use here.
}
});
//db = FirebaseFirestore.DefaultInstance;
//if (istrue)
//{
// savedata();
// istrue = false;
//}
}
public void savedata()
{
stuRef.Document("hello").GetSnapshotAsync().ContinueWith(task =>
{
if (task.IsCompleted)
{
task.GetAwaiter();
if (task.IsFaulted)
{
Debug.Log(task.Exception.Message);
}
Debug.Log("succesfully added to database");
Debug.Log(task);
DocumentSnapshot snapshot = task.Result;
if (snapshot.Exists)
{
Debug.Log("snapshot exists");
//user = snapshot.ToDictionary();
// foreach (KeyValuePair<string, object> pair in user)
// {
// Debug.Log(("{0}:{1}", pair.Key, pair.Value));
// }
}
else
{
Debug.Log("snapshot does not exist");
}
}
else
{
Debug.Log("failed db");
}
});
}
一些注意事項,應該可以幫助您調試:
Task
中的Exception
屬於AggregateException
類型。 這意味着不是用Debug.Log(task.Exception.Message);
,您應該遍歷它包含的異常(通常只有 1 在 Firebase 的情況下)並打印出每個內部異常。
我傾向於使用Flatten
方法記錄這些。 解釋微軟的文檔:
foreach (var e in task.Exception.Flatten().InnerExceptions) {
Debug.LogWarning($"Received Exception: {e.Message}");
}
您也可以使用Handle
。 再次解釋官方文檔:
task.Exception.Handle(e => {
Debug.LogWarning($"Received Exception: {e.Message}");
});
我懷疑一旦您相應地更改代碼,錯誤就會變得明顯。 但是,如果您仍然卡住,請隨時使用適當的異常文本更新錯誤。
此外:
Task.IsCompleted
,這在延續中始終是正確的(請參閱文檔)。Task.GetAwaiter
,這是供編譯器使用的(盡管如果您正在做一些有創意的事情,請不要讓我阻止您)。ContinueWith
替換為ContinueWithOnMainThread
,這是Firebase 提供的擴展方法,可確保繼續在主(Unity)線程上運行。 如果您在延續中UnityEngine
命名空間中的幾乎所有內容,您將立即遇到新的令人興奮的異常。我會確保快照存在於文檔中:
DocumentReference docRef = db.Collection("Users").Document("hello");
DocumentSnapshot snapshot = await docRef.GetSnapshotAsync();
if (snapshot.Exists)
{
//whatever
} else
{
Console.WriteLine("Document {0} does not exist!", snapshot.Id);
}
這樣就可以將文檔的存在作為問題丟棄。
我還會在問題中告訴您獲取錯誤的代碼中對應的CloudFirebase.cs:60
行是什么,如果它不是來自您的代碼,請嘗試提供從您的代碼中調用的最后一行產生錯誤的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.