UWP SQLite-PCL获取查询工作非常慢

时间:2016-10-21 12:25:15

标签: c# mysql sqlite uwp windows-10-universal

我正在编写Windows 10 Universal App(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毫秒,在手机设备上花了大约300-400毫秒(Lumia 930 - Windows Mobile 10)。我不知道为什么性能会有这样的差异。我有更多的Get方法,每个人从数据库获取信息太慢。当我必须在相同的xaml超过4秒的时间内使用3种或更多方法来显示本地机器上的内容时,在手机设备上的速度要快1秒。我想提高性能。

这就是我在app页面中使用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();

如果有人能帮我解决这个问题,我将不胜感激。 感谢。

1 个答案:

答案 0 :(得分:0)

不确定这是否是您问题的真正原因,但是您要为每个查询创建一个新SQLiteAsyncConnectionSQLiteConnectionWithLock。看起来像是浪费。尝试只创建一次。