UWP SQLite-PCL get查询工作非常慢
本文关键字:非常 工作 查询 SQLite-PCL get UWP | 更新日期: 2023-09-27 18:14:07
我正在写Windows 10通用应用程序(UWP)。简单的功能。我有本地SQLite数据库本地文件(.db)已经充满了记录。我基本上就是从中读取并显示信息。数据文件的表和结表很少,记录也不多,只有112行6列。这是我的DataBaseConnector类:
class DataBaseConnector
{
public const string dbName = "HeroData.db";
/* connection element for SQLite universal platform */
//private static string dbFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "HeroData.db");
Func<SQLiteConnectionWithLock> connectionFactory =
new Func<SQLiteConnectionWithLock>(
() =>
new SQLiteConnectionWithLock(
new SQLitePlatformWinRT(),
new SQLiteConnectionString(dbName, storeDateTimeAsTicks: false)));
private static DataBaseConnector db = new DataBaseConnector();
private SQLiteAsyncConnection GetDbConnectionAsync()
{
var asyncConnection = new SQLiteAsyncConnection(connectionFactory);
return asyncConnection;
}
public static DataBaseConnector dbInstance
{
get {
if ( db == null)
{
db = new DataBaseConnector();
}
return db;
}
}
public static List<Hero> AllHeroesList = new List<Hero>();
public DataBaseConnector()
{
}
//Get All Heroes From DataBase
public async Task<List<Hero>> getAllHeroes()
{
if (AllHeroesList.Count == 0) {
//< get records >
string query = @"SELECT hero.id, hero.name, hero.attack_id, hero.attribute_id, hero.role_id, attack.type AS type_name, attribute.name AS attribute_name FROM
Heroes hero, AttackType attack, Attribute attribute Where hero.attack_id = attack.id AND
hero.attribute_id = attribute.id";
var connection = this.GetDbConnectionAsync();
AllHeroesList = await connection.QueryAsync<Hero>(query);
}
return AllHeroesList;
}
当我运行它时,它工作得很好,但是有点慢,这个方法AllHeroesList();在本地机器(桌面)上花费约1400毫秒,在手机设备(Lumia 930 - Windows Mobile 10)上花费约300-400毫秒。我不知道为什么在表现上有这么大的差别。我有更多的Get方法,每一个从数据库获取信息太慢。当我必须使用3个或更多的方法在同一个xaml页面3x ~1400ms超过4秒来显示本地机器上的内容时,在电话设备上快得多~1秒。我想提高性能。
这是我如何使用SQL方法在应用程序页面:
EnableProgressRing();
if (ct != null && ct.Token.CanBeCanceled)
{
ct.Cancel();
}
ct = new CancellationTokenSource();
heroRoles.Text = await Task.Run(() => GetHeroRoles(ChosenHero.id), ct.Token);
IsWeakList.ItemsSource = await Task.Run(() => DataBaseConnector.dbInstance.GetWeakAgainst(heroID), ct.Token);
IsStrongList.ItemsSource = await Task.Run(() => DataBaseConnector.dbInstance.GetStrongAgainst(heroID), ct.Token);
DisableProgressRing();
如果有人能帮我解决那个问题,我会很感激的。谢谢。
不确定这是否是问题的实际原因,但是您正在为每个查询创建一个新的SQLiteAsyncConnection
和一个新的SQLiteConnectionWithLock
。看起来很浪费。