user1667474
user1667474

Reputation: 819

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

Upvotes: 3

Views: 4745

Answers (2)

rubStackOverflow
rubStackOverflow

Reputation: 6163

Xamarin Platforms Android Config, iOS and WP basicaly equal

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

Upvotes: 0

SKall
SKall

Reputation: 5234

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

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

Upvotes: 3

Related Questions