【问题标题】:Warning database has been locked for 0:00:10.000000. Make sure you always use the transaction object for database operations during a transaction警告数据库已锁定 0:00:10.000000。确保在事务期间始终使用事务对象进行数据库操作
【发布时间】:2021-02-08 20:27:21
【问题描述】:

我正在尝试从远程服务器获取图像。我不断收到此错误:

  Warning database has been locked for 0:00:10.000000. 
  Make sure you always use the transaction object for database operations during a transaction

然后我的应用程序崩溃了。


Widget carousel = new Container(
      height: 200,
      child: FutureBuilder<List<String>>(
          future: getPropImgs(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return new Carousel(
                
                boxFit: BoxFit.cover,
                images: snapshot.data.map((f) {
                  return new CachedNetworkImage(
                    imageUrl: f.toString(),
                    imageBuilder: (context, imageProvider) => Container(
                      decoration: BoxDecoration(
                        image: DecorationImage(
                            image: imageProvider,
                            fit: BoxFit.cover,
                            colorFilter:
                                ColorFilter.mode(Colors.blue, BlendMode.dstIn)),
                      ),
                    ),
                    placeholder: (context, url) => Center(
                        child: Container(
                            height: 30,
                            width: 30,
                            child: CircularProgressIndicator())),
                    errorWidget: (context, url, error) =>
                        Image.asset("assets/images/icon.png"),
                  );
                }).toList(),
                autoplay: true,
                animationCurve: Curves.fastOutSlowIn,
                animationDuration: Duration(milliseconds: 1000),
                indicatorBgPadding: 1.0,
                dotColor: Colors.black,
                
              );


【问题讨论】:

  • 能否把相关代码分享给数据库连接?
  • 如果解决了您的问题,请回复。

标签: android flutter


【解决方案1】:

我猜你正在尝试执行大量的 SQL 查询。 一旦缓慢的解决方案是确保您等待每个电话。在此处的第二行中,您缺少等待:

 await db.transaction((txn) async {

但是在这里它仍然会在每次插入时创建一个事务,这样会非常慢。一种解决方案是使用批处理 (https://github.com/tekartik/sqflite/tree/master/sqflite#batch-support) 并将插入语句排队(例如 1000 x 1000);

var batch = db.batch();
batch.rawInsert(statement1);
batch.rawInsert(statement2);
batch.rawInsert(statement3);
...
await db.commit(noResult: true);

【讨论】:

  • 我找到的关于这个问题的第一个简单而有用的答案;谢谢,它帮了很多忙,@Ömer!
猜你喜欢
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
相关资源
最近更新 更多