I created a class for managing the SQLite database in my MAUI app. I have an async method to initialize it:
public static async Task InitializeDatabase()
{
_dbPath = Path.Combine(FileSystem.AppDataDirectory, DB_NAME);
_connection = new SQLiteAsyncConnection(_dbPath, _openFlags);
try
{
var docTableResult = await _connection.CreateTableAsync<Document>();
var itemTableResult = await _connection.CreateTableAsync<DocumentItem>();
var articleTableResult = await _connection.CreateTableAsync<Article>();
}
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
I'm calling this method from MauiProgram, after registering my services. When I call it by creating a new task, it runs fine:
Task.Run(async () => await SQLiteService.InitializeDatabase());
But if I call it synchronously, it crashes on the line where it creates the first table (Document) and shows no error.
SQLiteService.InitializeDatabase().GetAwaiter().GetResult();
Why does it crash?