Xamarin.Forms using SQLite.Net.Async

自古美人都是妖i 提交于 2020-03-22 08:42:25

问题


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


回答1:


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

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



回答2:


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


来源:https://stackoverflow.com/questions/25945223/xamarin-forms-using-sqlite-net-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!