dart - 使用 rootBundle.load() 阻塞 UI 的大型数据库负载
问题描述
我在资产文件夹中有一个大型数据库(大约 130mb)。但是当我使用 rootBundle.load() 时,UI 会阻塞 3-4 秒。有什么解决办法吗?
代码:
Future<Database> initDb() async {
var databasesPath = await getDatabasesPath();
var path = join(databasesPath, "hadith_db.db");
bool fileExists = File(path).existsSync();
if (!fileExists) {
// Should happen only the first time you launch your application
print("Creating new copy from asset");
// Copy from asset
print("starting root bundle");
ByteData data = await rootBundle.load(join("assets", "hadith_db.db"));
print("starting buffer");
List<int> bytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
print("finished buffer");
await File(path).writeAsBytes(bytes);
}
// open the database
Database db = await openDatabase(path, readOnly: true);
return db;
}
注意:为了显示这些数据,我正在使用 FutureBuilder
解决方案
您可以使用飞镖隔离物。Dart 隔离用于并发编程,并创建类似于线程但不共享内存的工作人员,它们仅通过消息传递工作。有关更多信息,请参阅:https ://api.dartlang.org/stable/2.2.0/dart-isolate/dart-isolate-library.html
推荐阅读
- java - 正则表达式模式 - 查询
- node.js - sage 8.5.1 中的 gulp watch 问题
- mongodb - kubernetes 更新后,kubernetes 上的 mongodb StatefulSet 不再工作
- python - 将项目添加到类实例中的列表中
- java - 连接器 J 安装
- r - R 和指数,标准二阶
- reactjs - [Webpack][React] 在带有代码拆分的 SSR 上,我如何获取页面所需的块列表?
- c# - 应用程序获得/失去焦点事件
- flutter - 在 Flutter 的 Stepper 中,如何将 Step 的标题放在 counter 下方?
- javascript - jQuery 不会使用 2 个选择器