SQLite挂起在CreateTablesAsync上 - Xamarin Forms

时间:2016-04-12 17:25:37

标签: c# sqlite asynchronous xamarin xamarin.forms

我正在尝试使用SQLite在我的应用程序中创建一个Account表,但是应用程序在CreateTableAsync调用期间挂起而不会抛出错误。

LocalDataContext.Instance.CreateTablesAsync(typeof(Account)).Wait();

var account = LocalDataContext.Instance.GetFirstOrDefaultAsync<Account>().Result;

因此,应用程序进入第一个调用,并且永远不会到达第二行。

以下是CreateTablesAsync方法的代码:

/// <summary>
    /// Creates the local database tables.
    /// </summary>
    public async Task CreateTablesAsync(params Type[] tableTypes)
    {
        await _connection.CreateTablesAsync(tableTypes);
    }

此挂起时输出中的最后一行是Found as 'sqlite3_column_int'.

这是Account模型:

    public class Account
    {
    /// <summary>
    /// Primary key for record.
    /// </summary>
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    /// <summary>
    /// Gets or sets the Client ID.
    /// </summary>
    public string ClientId { get; set; }

    /// <summary>
    /// Gets or sets the User ID.
    /// </summary>
    public string UserId { get; set; }

    /// <summary>
    /// Gets or sets the user password.
    /// </summary>
    public int Password { get; set; }
}

有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

您使用SQLite.Net Async但尝试同步调用它的任何特殊原因?您应该拨打await LocalDataContext.Instance.CreateTablesAsync(typeof(Account))而不是同步等待。你可能在某个地方陷入僵局。

如果 要将其称为同步,那么您需要使用SQLite.Net(非异步版本)或从其他位置调用create table方法。

此外,如果这是其中唯一的代码,您可以将CreateTablesAsync更改为:

public Task CreateTablesAsync(params Type[] tableTypes)
{
    return _connection.CreateTablesAsync(tableTypes);
}

就个人而言,我遇到了类似的问题,需要在非异步代码中调用异步方法。我最终只是使用同步版本而没有问题。