1

First, this is the code:

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

Ok, so my problem is that after the "....ContinueWith(task => ..." ' j ' will become directly equal to the count variable. Why this happens and how to solve it? Or is there another method to do that?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Meqara
  • 23
  • 5

1 Answers1

1

Ok, so my problem is that after the "....ContinueWith(task => ..." ' j ' will become directly equal to the count variable. Why this happens and how to solve it?

That's because you used <= instead of <. With <=, j must be equals to count for the loop condition to be met and finish. If you want j to be less than count then use count-1 or simply use <.

So, that should be

for (int j = 1; i <= count-1; j++)

Or

for (int j = 1; i < count; j++)

Note that array starts from 0 not 1 so int j = 1; should be int j = 0; but I have a feeling that's what you wanted to do and you are starting the loop from 1 on purpose.


Finally, another problem is your variable being captured in a loop because you are using lambda in the ContinueWith function. See this post for more information. To use the j variable inside the ContinueWith lambda function, make a copy of it then use that copy instead of the j variable.

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 
}

Complete fixed code:

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
        }
    });
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Not that what I mean. What I mean is that the ' j ' variable directly jump to 'count' even if it is (j < count) or (j <= count), so it will not be ( j = count - 1) for example, only ( j = count). – Meqara May 26 '18 at 23:13
  • I know what you mean. Read my answer again. Read it again. See the second part where I also mentioned about "variable being captured" and making a copy of it to fix. Also, there was a typo in your question with `i` . I have the-same typo since I copied it directly from your question but just change that `i` to `j`. It's just a typo. – Programmer May 26 '18 at 23:21
  • Ok, I think I get your idea, but you need to put the "int jCopy = j;" before "db.child("some...", and then after it we add "j = jCopy;" , to get it works. This solves the problem, but do you know why this happens? And thank you. – Meqara May 27 '18 at 01:06