multiGet method

Future<List<StorageKeyValue>?> multiGet(
  1. VoltronArray keys
)
override

Implementation

Future<List<StorageKeyValue>?> multiGet(VoltronArray keys) async {
  var database = await _sqLiteHelper.getDatabase();
  if (database == null) {
    throw 'Database Error';
  }
  var columns = <String>[SQLiteHelper.kColumnKey, SQLiteHelper.kColumnValue];
  var finalData = <dynamic>[];
  var resultList = <StorageKeyValue>[];
  StorageKeyValue kv;
  try {
    // 批量获取 一次性最多获取MAX_SQL_KEYS条
    for (var keyStart = 0; keyStart < keys.size(); keyStart += kMaxSqlKeys) {
      var keyCount = min(keys.size() - keyStart, kMaxSqlKeys);
      var args = <String>[];
      for (var i = 0; i < keyCount; i++) {
        var keyStr = keys.get<String>(keyStart + i);
        if (keyStr != null) {
          args.add(keyStr);
        }
      }
      var batch = database.batch();
      for (var arg in args) {
        batch.query(
          _sqLiteHelper.getTableName(),
          columns: columns,
          where: '${SQLiteHelper.kColumnKey} = ?',
          whereArgs: [arg],
        );
      }
      List<dynamic> result = await batch.commit();
      finalData.addAll(result);
    }
    // 将结果封装一下 以map的形式返回
    for (List list in finalData) {
      for (Map<String, dynamic> map in list) {
        kv = StorageKeyValue(
          map[SQLiteHelper.kColumnKey],
          map[SQLiteHelper.kColumnValue],
        );
        resultList.add(kv);
      }
    }
    return resultList;
  } catch (e) {
    LogUtils.e('flutterRender', e.toString());
    return null;
  } finally {
    //todo 一些关闭操作
  }
}