Home > Mobile >  how to input value in this function?
how to input value in this function?

Time:12-27

i want to input some data in aws QueryAsync fucntion but it doesn't work

how can i do that?

List<int> mNum = new List<int>();

mNum.Add(1);
mNum.Add(2);
mNum.Add(3);

client1.QueryAsync(request, (queryResult) =>
{
    if(queryResult.Exception != null)
    {
        print(queryResult.Exception.ToString());
        return;
    }

    foreach(var i in queryResult.Response.Items){ ... }

    mNum.Add(4);
});

for(int i = 0; i < mNum.Count; i  ) print (mNum[i]); // count -> 3

enter image description here

CodePudding user response:

Your mistake is your code outside of the lambda expression is executed immediately before the query has finished so your line

mNum.Add(4);

is executed asynchronous long after the for loop was already executed and printed out the only 3 elements that existed at that moment.

You would rather have to wait for the callback and do it within the lambda like e.g.

List<int> mNum = new List<int>();

mNum.Add(1);
mNum.Add(2);
mNum.Add(3);

client1.QueryAsync(request, (queryResult) =>
{
    if(queryResult.Exception != null)
    {
        print(queryResult.Exception.ToString());
        return;
    }

    foreach(var i in queryResult.Response.Items){ ... }

    mNum.Add(4);

    foreach(var num in mNum)
    {
        print (num);
    }
});
  • Related