2

在我的 Windows 8 Metro 应用程序中,我在 SQLite 数据库中有一个表,有时必须使用大量记录(大约 500 到 600 条)进行更新。我从 Web 服务获取记录,然后遍历记录并将它们插入到表中。问题是这个操作大约需要 10 到 15 秒,并且它会锁定包括进度条在内的 UI。代码如下。

await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
    foreach (var item in body3)
    {
        db.Insert(new Site
           {
              siteName = item.siteName,
              siteAddress = item.siteAddress,
              siteCity = item.siteCity,
              siteState = item.siteState,
              siteID = item.siteID
           });
        progressBar.Value = i;                                
        i++;
    }
});

我认为这是 SQLite 的问题。我似乎也无法从中捕获异常。

我认为我真正的问题是如何让它在另一个线程中正确运行,这样它就不会影响 UI。我并不担心插入记录需要一段时间。我只是希望用户界面保持响应。

4

2 回答 2

3

--不要从循环中的每个插入更新你的用户界面......如果需要的话,可能每 20% 更新一次

-- 使用事务 -- http://www.sqlite.org/faq.html #19 和这里提高 SQLite 的每秒插入性能?

于 2013-06-06T21:45:25.067 回答
0

以这种方式尝试。

foreach (var item in body3)
{
    db.RunInTransaction(() =>
    {
        db.Insert(new Site
        {
            siteName = item.siteName,
            siteAddress = item.siteAddress,
            siteCity = item.siteCity,
            siteState = item.siteState,
            siteID = item.siteID
        });
    });
    await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        progressBar.Value = i;                                
        i++;
    });
}
于 2013-06-07T02:14:19.717 回答