3

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

2 Answers 2

3

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

var asyncDb = new SQLiteAsyncConnection(() => GetConnection());
Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou - and for anyone else with the same problem, there is a solution here (captechconsulting.com/blog/nicholas-cipollina/…) that also works
Dead link please delete or change,
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.