Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin.Forms using SQLite.Net.Async

I have followed the instructions here http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/ - to connect to the SQLite database synchronously.

public SQLiteConnection GetConnection()
{
    var dbFilname = "localDB.db3";
    string docsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    var path = Path.Combine(docsPath, dbFilname);

    var plat = new SQLitePlatformAndroid();
    var conn = new SQLiteConnection(plat, path);
    return conn;
}

I want to change it to an asynchronous connection (SQLiteAsyncConnection) but can't get it to work.

According to the instructions here - https://components.xamarin.com/gettingstarted/sqlite-net -it just needs the path as a parameter

var conn = new SQLiteAsyncConnection(path);

that doesn't work, the error says that the parameters expected are:

a connection function, a TaskScheduler and TaskCreationOptions

I have no idea what to do and have not been able to find any examples that work.

Thanks in advance

like image 360
user1667474 Avatar asked Dec 08 '25 10:12

user1667474


1 Answers

You could simply reuse the GetConnection method you already have and create async connection like this:

var asyncDb = new SQLiteAsyncConnection(() => GetConnection());
like image 72
SKall Avatar answered Dec 11 '25 02:12

SKall