Firebase(…..ContinueWith(task=>;…)在For循环中

Firebase(…..ContinueWith(task=>;…)在For循环中,firebase,for-loop,unity3d,firebase-realtime-database,Firebase,For Loop,Unity3d,Firebase Realtime Database,首先,这是代码: for (int j = 1; j <= count; j++) { db.Child("Some Child").GetValueAsync().ContinueWith(task => { Debug.Log("j: " + j); // Here the Debug will show me that j = count if (task.IsFaulted)

首先,这是代码:

 for (int j = 1; j <= count; j++)
 {    
      db.Child("Some Child").GetValueAsync().ContinueWith(task =>
      {
           Debug.Log("j: " + j); // Here the Debug will show me that j = count
           if (task.IsFaulted)
           {
               // ERROR HANDLER
           }
           else if (task.IsCompleted)
           {
               // Some Code Here
            }
        });
  }
for(int j=1;j
{
Debug.Log(“j:+j);//这里的调试将显示j=count
if(task.IsFaulted)
{
//错误处理程序
}
else if(任务已完成)
{
//这里有一些代码
}
});
}
好的,我的问题是在“…ContinueWith(task=>…”之后,“'j'将直接等于count变量。为什么会发生这种情况以及如何解决它?或者有其他方法可以做到这一点吗

好的,我的问题是在“…ContinueWith(task=>…””j之后 '将直接等于count变量。为什么会发生这种情况 如何解决呢


那是因为你使用了
而不是我的意思。我的意思是'j'变量直接跳到'count',即使它是(ji
。我也有同样的输入错误,因为我直接从你的问题中复制了它,但只是把
i
改成
j
。这只是一个输入错误。好吧,我想我明白你的意思,但你需要在“db.child”(“一些。。。,然后在它后面加上“j=jCopy;”,让它工作。这解决了问题,但是你知道为什么会发生这种情况吗?谢谢。
for (int j = 1; i <= count-1; j++)
for (int j = 1; i < count; j++)
db.Child("Some Child").GetValueAsync().ContinueWith(task =>
{

    //MAKE A COPY OF IT
    int jCopy = j;
    Debug.Log("j: " + jCopy); // Here the Debug will show me that j = count 
}
for (int j = 1; i < count; j++)
{
    db.Child("Some Child").GetValueAsync().ContinueWith(task =>
    {
        //MAKE A COPY OF IT
        int jCopy = j;
        Debug.Log("j: " + jCopy);

        if (task.IsFaulted)
        {
            // ERROR HANDLER
        }
        else if (task.IsCompleted)
        {
            // Some Code Here
        }
    });
}