Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite hangs on CreateTablesAsync - Xamarin Forms

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?

like image 480
cfly24 Avatar asked Oct 25 '25 22:10

cfly24


1 Answers

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.

like image 145
valdetero Avatar answered Oct 28 '25 12:10

valdetero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!