遍历 DataSnapShot.Children 会停止代码的执行(Unity、Firebase) [英] Iterating through DataSnapShot.Children stops the execution of code (Unity, Firebase)

查看:23
本文介绍了遍历 DataSnapShot.Children 会停止代码的执行(Unity、Firebase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一种从 Unity (C#) 中的 Firebase 检索数据的方法.我可以成功检索数据.但是,当我遍历 dataSnapShot.Children 以将值分配给我将在游戏中使用的某些变量时.执行被停止.控制台中没有错误.

I'm writing a method that retrieves the data from Firebase in Unity (C#). I can successfully retrieve the data. But when I iterate through dataSnapShot.Children to assign values to my certain variables that are to be used in the game. The execution gets stopped. There is no error in Console.

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();
            }
  });
}

推荐答案

我以某种方式做了一个变通办法来修复它.实际上,norelSnapshot 由 Firebase Unity SDK 提供)属于 IEnumerable.类型.我在互联网上搜索了 迭代 IEnumerable 集合.然后从这个替换我的代码:

I somehow did a workraround to get it fixed. Actually the SnapShot.Children (by Firebase Unity SDK) is of IEnumerable. Type. I searched through internet on iterating IEnumerable collections. And then replaced my code from this :

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 


         }

它就像一个魅力...我的理解是这个执行是在一个线程任务"中完成的.虽然,我不知道它究竟是如何工作的,或者为什么它不能与我以前的代码一起工作.欢迎可以提供更好信息的人:)干杯!

It worked like a Charm... What I understand is that this execution was being done a Threaded "Task". Although, I don't know exactly how is it working or why it was not working with my previous code. Someone, who can provide better information is welcome :) Cheers!

这篇关于遍历 DataSnapShot.Children 会停止代码的执行(Unity、Firebase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆