xamarin forms await is not respected

6

Good evening,

Can someone tell me why the code below does not respect Wait, in debug the output order is

    1. init save
    2. End Save 3. New header ID: 1

instead of:

    1. init save
    2. New header ID: 1
    3. End Save
public async Task Save(Ticket header, TicketLines newLines)
{
                Debug.WriteLine("init save");
                await dbConnection.RunInTransactionAsync(new Action<SQLite.Net.SQLiteConnection>(tran =>
                {
                    dbConnection.InsertAsync(header).ContinueWith((t) =>
                    {
                        Debug.WriteLine("New header ID: {0}", header.Id);                       
                        foreach (var item in newLines)
                        {
                            item.DocumentId = header.Id;
                        }
                        dbConnection.InsertAllAsync(newLines);                     
                    });
                    //tran.Commit();                    
                }));
                Debug.WriteLine("End Save");

}

I want to call the Save method and in the end I command refresh the screen, what happens as it does not respect the Await I have to refresh the screen and still not insert the data

    
asked by anonymous 11.10.2016 / 20:48

1 answer

1

In fact, what is happening is that you are not expecting the InsertAsync function, so program follows normal flow and prints End Save , as expected.

The output you want requires that the InsertAsync call has ended, and ContinueWith has started. To do this, simply enter a await before dbConnection.InsertAsync . This will stop the execution of this method until ContinueWith is completed.

Warning: dbConnection.InsertAsync(...).ContinueWith(...) returns a awaitable that refers to the ContinueWith call, not InserAsync .

    
02.11.2016 / 03:56