Xamarin形成sqlite异步等待不等待进程

时间:2016-10-11 19:57:43

标签: c# sqlite asynchronous xamarin.forms

因为它不尊重等待

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

}

订购调试

  1. init save
    1. 结束保存sds
    2. - &gt;新标题ID:1

1 个答案:

答案 0 :(得分:-1)

你有两个没有等待的异步调用(你之前没有await关键字)

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

}