Win10 UWP 开发系列:支持异步的SQLite

上篇文章已经实现了在UWP中使用SQLite作为本地存储,作为移动端的程序,及时响应用户的操作是提高用户体验的重要途径,因此UWP的很多api都是异步的。那么如何使SQLite支持异步呢?


参考SQLite.Net-PCLgithub页面:https://github.com/oysteinkrog/SQLite.Net-PCL


可以看到SQLite.Net-PCL是支持异步的,在创建数据库链接的时候,可以创建同步的SQLiteConnection,也可以创建异步的SQliteAsyncConnection


SQliteAsyncConnection

The SQLiteAsyncConnection class now takes a Func in the constructor instead of a path. This is done because the async classes are now just meant to be wrappers around the normal sqlite connection.

To use SQLiteAsyncConnection just create an instance of a SQLiteConnectionWithLock and pass in that through a func, e.g.: new SQLiteAsyncConnection(()=>_sqliteConnectionWithLock);

Please be aware that the Task.Run pattern used in SQLiteAsyncConnection can be considered an anti-pattern (libraries should not provide async methods unless they are truly async). This class is maintained for backwards compatability and for use-cases where async-isolation is handy.


在之前的版本中,创建SQLiteAsyncConnectionSQLiteConnection的写法是类似的,都是传入一个数据库文件地址即可,但新版本中异步的构造函数有点变化,需要传入一个Func


接下来我们看一下如何使用异步的方式来使用SQLite


一、添加SQLite.Net.Async-PCL支持


还是在上个例子里直接改吧,首先我们之前添加的SQLite.Net-PCL是不支持异步的,需要添加另一个nuget包:



装了这个就可以使用异步的了。


二、创建异步的数据库链接


public SQLiteAsyncConnection GetDbConnectionAsync()

 

{

var connectionFactory = new Func<SQLiteConnectionWithLock>(() => new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString(DbFilePath, storeDateTimeAsTicks: false)));

var asyncConnection = new SQLiteAsyncConnection(connectionFactory);

return asyncConnection;

把初始化方法改为:

public async Task InitAsync()

 

{

 

DbFilePath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, DbFileName);

var db = GetDbConnectionAsync();

await db.CreateTableAsync<UserItem>();

 

}

注意要在StartupFunctions.cs文件里调用这个异步的,把原来那个Init方法注释掉。


三、异步访问数据库


其实之后就没有太多可说的了,就是把原来的同步方法改成异步的就可以了,比如插入数据:

public async Task<int> InsertUserAsync(UserItem item)

 

{

int result = 0;

var conn = DbContext.Instance.GetDbConnectionAsync();

 

result = await conn.InsertAsync(item);

return result;

 

}

获取全部数据:

public async Task<List<UserItem>> GetAllUserAsync()

 

{

 

List<UserItem> result = new List<UserItem>();

var conn = DbContext.Instance.GetDbConnectionAsync();

 

result = await conn.Table<UserItem>().ToListAsync();

return result;

 

}



 查询的话可以这样:

public async Task<List<UserItem>> GetUserListAsync(string key)

 

{

 

List<UserItem> result = new List<UserItem>();

var conn = DbContext.Instance.GetDbConnectionAsync();

 

result = await conn.Table<UserItem>().Where(x => x.UserName.Contains(key)).ToListAsync();

return result;

 

}

其他几个UpdateDelete也有相应的异步方法,就不写了。

还有几个方法是QueryAsyncExecuteAsyncExecuteScalarAsync等等,都可以直接执行sql语句,例如:

public async Task<int> GetUserCount()

 

{

var conn = DbContext.Instance.GetDbConnectionAsync();

return await conn.ExecuteScalarAsync<int>("select count(*) from UserItem");

 

}

建议使用异步的方式以获得更好的性能。


开发者交流群:53078485期待您的加入!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值