Xamarin.Forms using SQLite.Net.Async

I followed the instructions here http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/ - for synchronous connection to SQLite database.

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 asynchronous connection (SQLiteAsyncConnection) but can't get it to work.

As per the instructions here - https://components.xamarin.com/gettingstarted/sqlite-net - it only needs the path as a parameter

var conn = new SQLiteAsyncConnection(path);

      

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

connection function, TaskScheduler and TaskCreationOptions

I don't know what to do and could not find any examples that work.

Thank you in advance

+3


source to share


2 answers


You can simply reuse the GetConnection method you already have and create an asynchronous connection like this:



var asyncDb = new SQLiteAsyncConnection(() => GetConnection());

      

+3


source


Xamarin Android platform for Android , iOS and WP based on principle



public SQLiteAsyncConnection GetConnectionAsync()
        {
            const string fileName = "MyDB.db3";
            var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var path = Path.Combine(documentsPath, fileName);

            var platform = new SQLitePlatformAndroid();

            var param = new SQLiteConnectionString(path, false); 
            var connection = new SQLiteAsyncConnection(() => new SQLiteConnectionWithLock(platform, param)); 

            return connection;
        }

      

0


source







All Articles