【问题标题】:For-loop stops when calling function in Firebase for Unity C#在 Firebase for Unity C# 中调用函数时,for 循环停止
【发布时间】:2020-12-09 19:59:24
【问题描述】:

我有与Iterating through DataSnapShot.Children stops the execution of code (Unity, Firebase) 类似的问题 这个问题的解决方案也帮助我让我的代码正常工作。但是,我还想调用一个函数,该函数在 for 循环的每次迭代中创建一个新的 GameObject。如果函数仅包含“Debug.Log”语句,for 循环似乎在每次迭代时都会调用 callForEachElement() 函数。一旦我尝试在函数内部实例化一个对象,for 循环就会在第一次调用后停止并且不会实例化该对象。在下面的代码中调用了 Debug.Log("Before instantiation") 并且从不调用 Debug.Log("After instantiation")。如果我想这样做,谁能解释为什么这不起作用以及为什么循环只运行一次?

public GameObject prefab;

private void loadEntriesFromServer()
    {
        FirebaseDatabase.DefaultInstance.GetReference("202012").GetValueAsync().ContinueWith(task =>
         {
             if (task.IsFaulted)
             {
                 // Handle the error...
                 Debug.Log("ERROR IN LOAD ENTRIES");
             }
             else if (task.IsCanceled)
             {
                 Debug.Log("Task was Cancelled");
             }
             else if (task.IsCompleted)
             {
                 DataSnapshot snapshot = task.Result;

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

                             }
                             catch (System.Exception e)
                             {
                                 Debug.Log(e.Message);
                             }
                         }
                     }
                 }
             }
         });


    void callForEachElement()
    {
        Debug.Log("Before instantiation");
        GameObject obj = Instantiate(prefab) as GameObject;
        Debug.Log("After instantiation");
    }
        
    

【问题讨论】:

  • GameObject obj = Instantiate(prefab) as GameObject;
  • 是的,正确的。看起来上面的行 -> Debug.Log("Before instantiation"); 是最后一个被执行的行。

标签: c# firebase loops unity3d


【解决方案1】:

不能保证ContinueWith在主线程上运行,你只能在主线程上安全地Instantiate。 考虑将您的实例化循环移到任务之外,或尝试使用 Firebase 扩展 ContinueWithOnMainThread,描述为 here

例如

FirebaseDatabase.DefaultInstance.GetReference("202012").GetValueAsync().ContinueWithOnMainThread(task

【讨论】:

  • 正确,是线程的问题!感谢您的代码示例,完美运行! :)
【解决方案2】:

我看起来像您在 Instantiate(prefab) 语句中执行的操作可能会导致来自 FirebaseDatabase.DefaultInstance.GetReference("202012").GetValueAsync() 的任务及其子任务被取消。

使用取消令牌调用ContinueWith() 方法,以便查看是否是这种情况。

Task Canellation的信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多