如何更快地获取和存储数据?

时间:2019-07-15 04:47:56

标签: flutter dart dart-http

当前时间是15秒以获取和存储。我调用了15个API。打电话之前我删除了表。反正是一次调用所有15个API并同时存储数据吗?

  await Future.wait([
    HelperDatabase().deleteAssetAssembly(),
    HelperDatabase().deleteEquipmentReg(),
    HelperDatabase().deleteGetUserPreference(),
    HelperDatabase().deleteSignIn(),
    HelperDatabase().deleteDefCatMaster(),
    HelperDatabase().deleteDefRegisterCat(),
    HelperDatabase().deleteDefCatRelation(),
    HelperDatabase().deleteWoDescription(),
    HelperDatabase().deleteCategoryDefect(),
    HelperDatabase().deleteRegister(),
    HelperDatabase().deleteSubmitRequest(),
    HelperDatabase().deleteWorkTypes(),
    HelperDatabase().deleteWorkSource(),
    HelperDatabase().deletePriorities(),
    HelperDatabase().storeRegister(_url, tokens),
    HelperDatabase().storeEquipmentReg(_url, tokens),
    HelperDatabase().storeGetUserPreference(_url, tokens),
    HelperDatabase().storeDefRegisterCat(_url, tokens),
    HelperDatabase().storeDefCatMaster(_url, tokens),
    HelperDatabase().storeDefCatRelation(_url, tokens),
    HelperDatabase().storeWoDescription(_url, tokens),
    HelperDatabase().storeAssetAssembly(_url, tokens),
    HelperDatabase().storeCategoryDefect(_url, tokens),
    HelperDatabase().storeWorkSource(_url, tokens),
    HelperDatabase().storeWorkTypes(_url, tokens),
    HelperDatabase().storePriorities(_url, tokens),
    HelperDatabase().storeSignIn(1),
    HelperDatabase().insertSync(_url, tokens),
    HelperDatabase().insertToken(token)
  ]);

所有15种 store方法不仅是存储方法,而且是 delete方法

示例存储方法,

  Future<void> storeRegister(String url, String token) async {
    var db = await db1;
    Batch batch = db.batch();
    final response = await http.get(
      '$url/nativeapi/v1.0/Register',
      headers: {'Authorization': 'Bearer $token'},
    );

    final jsonResponse = json.decode(response.body);
    AssetRegister model = AssetRegister.fromJson(jsonResponse);
    int length = model.data.length;
    for (int i = 0; i < length; i++) {
      var data = DataRegister(
          i: model.data[i].i,
          d: model.data[i].d,
          c: model.data[i].c,
          u: model.data[i].u,
          p: model.data[i].p,
          a: model.data[i].a);
      batch.insert(
        'registerTable',
        data.toMap(),
        //conflictAlgorithm: ConflictAlgorithm.replace,
      );
    }
    await batch.commit();
  }

示例删除方法,

  Future<void> deleteAssetAssembly() async {
    var db = await db1;
    await db.delete('AssetAssemblyTable');
  }

0 个答案:

没有答案