我正在编写一种以Unity(C#)从Firebase检索数据的方法。我可以成功检索数据。但是,当我通过datasnapshot.Children迭代将值分配给游戏中要使用的某些变量。执行停止。控制台没有错误。
public void GetUsers(List<User> users)
{
FirebaseDatabase.DefaultInstance
.GetReference("users")
.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted) {
// Handle the error...
Debug.Log("Error was:"+task.Exception.Message);
Debug.LogError("Error was:"+task.Result.Children);
}
else if (task.IsCompleted) {
DataSnapshot snapshot = task.Result;
// Do something with snapshot...
foreach(DataSnapshot s in snapshot.Children){
IDictionary dictUsers = (IDictionary)s.Value;
Debug.Log(dictUsers["displayName"]);
}
// After this foreach loop in snapshot.Children, nothing executes
UIManager.instance.ShowOtherUsers();
}
});
}
i以某种方式进行了工作以修复它。实际上,快照(Firebase Unity SDK)是可以的。类型。我通过Internet搜索了Inumerable系列。然后从此替换我的代码:
else if (task.IsCompleted) {
DataSnapshot snapshot = task.Result;
// Do something with snapshot...
foreach(DataSnapshot s in snapshot.Children){
IDictionary dictUsers = (IDictionary)s.Value;
Debug.Log(dictUsers["displayName"]);
}
// After this foreach loop in snapshot.Children, nothing executes
UIManager.instance.ShowOtherUsers();
}
:
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
// Do something with snapshot...
using (var sequenceEnum = snapshot.Children.GetEnumerator())
{
for(int i = 0 ;i<snapshot.Children.Count();i++){
while (sequenceEnum.MoveNext())
{
try{
IDictionary dictUser =(IDictionary)sequenceEnum.Current.Value;
Debug.Log("displayName:"+dictUser["displayName"]);
}
catch(System.Exception e){
Debug.Log(e.Message);
}
Debug.Log("At The End!");
UIManager.instance.ShowOtherUsers(); // Now it executes like a Charm
}
它像魅力一样工作……我知道该执行正在完成一个螺纹"任务"。虽然,我不知道它是如何工作的,或者为什么它不使用我以前的代码。欢迎某人,可以提供更好的信息:)欢呼!
我认为这两者都是混合的,例如当您初始化SDK时,它在不是主线程的线程中如何完成,如果您的Internet Connection不足以保持CPU速度,主线程和工作线程在种族条件下输入,无法按照Unity Monobehaviours的方式进行跟进(根本不是多线程友好)。
Remenber同样允许使用JSON格式的TOPYC,只有几种本地类型支持。