【问题标题】:SQLite hangs on CreateTablesAsync - Xamarin FormsSQLite 在 CreateTablesAsync 上挂起 - Xamarin Forms
【发布时间】:2016-08-03 11:18:27
【问题描述】:

我正在尝试使用 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; }
}

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

【问题讨论】:

  • 使用Result 而不是仅仅运行任务并使用await LocalDataContext.Instance.GetFirstOrDefaultAsync&lt;Account&gt;() 很可能是死锁。您可以使用Task.Run 创建表,然后像上面那样简单地等待来检索。 stackoverflow.com/questions/13140523/…
  • 如果你读过它,你犯了一个错误,你正在使用 Xamarin.. :) 使用 swift 并忘记 xamarin.. 相同的 API 但稳定且有效.. 看到 @987654332 令人难以置信@, nuint 等等。非常非常糟糕的代码库

标签: c# sqlite asynchronous xamarin xamarin.forms


【解决方案1】:

您使用 SQLite.Net Async 但尝试同步调用它的任何特殊原因?您应该调用await LocalDataContext.Instance.CreateTablesAsync(typeof(Account)) 而不是同步等待。您可能在某个地方遇到了死锁。

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    • 2019-03-20
    • 1970-01-01
    • 2021-12-03
    相关资源
    最近更新 更多