3

I am trying to create an Account table in my application using SQLite, but the application hangs during the CreateTableAsync call without throwing an error.

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

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

So, the application goes into the first call and never reaches the second line.

Here is the code for the CreateTablesAsync method:

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

The last line in the output when this hangs is Found as 'sqlite3_column_int'.

And here is the Account model:

    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; }
}

Anybody know why this may be happening?

cfly24
  • 1,882
  • 3
  • 22
  • 56
  • Most likely a deadlock using `Result` instead of just running the Task and using `await LocalDataContext.Instance.GetFirstOrDefaultAsync()` instead. You can use `Task.Run` to create the tables and then a simple await like above to retrieve. http://stackoverflow.com/questions/13140523/await-vs-task-wait-deadlock – Jon Douglas Apr 12 '16 at 17:36
  • if you read it, you did a mistake, you're using Xamarin.. :) use swift and forget about xamarin.. the same API but stable and works.. it's unbelievable to see `nint`, `nuint` etc.. very very bad codebase – GSerjo Jun 28 '20 at 04:42

1 Answers1

2

Any particular reason that you are using SQLite.Net Async but trying to call it synchronously? You should call await LocalDataContext.Instance.CreateTablesAsync(typeof(Account)) instead of synchronously waiting. You are probably hitting a deadlock somewhere.

If you have to call it sync, then you need to either use SQLite.Net (non-async version) or call your create table method from a different location.

Also, if this really is the only code in it, you can change CreateTablesAsync to:

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

Personally, I had similar issues with needing to call the async methods in non-async-able code. I ended up just going with the sync version and had no problems.

valdetero
  • 4,624
  • 1
  • 31
  • 46