How to handle database asynchronous operation in ionic 2/3(如何在 ionic 2/3 中处理数据库异步操作)
问题描述
我正在使用 ionic 中的数据库,我调用了一个返回许多记录的 API,我必须将这些记录插入数据库以及何时完成插入操作然后我想要从数据库中调用选择记录.问题是异步行为,在插入操作完成之前调用数据库中的选择记录.谁能帮我解决这个问题?我的代码在下面...
I am working with the database in ionic, I call one API that returns me a number of records, I have to insert those records into the database and when to insert operations are completed then I want to call select records from the database. problem is asynchronous behavior, the select records from the database called before the insert operations are completed. can anyone help me to resolve this? my code is below...
数据库提供者:
export class DbProvider {
public addData(dId: string, sId: string, subId: string, subName: string,
dDate: string, cId: string, cName: string, stdId: string, stdName: string,
) {
return new Promise((resolve, reject) => {
this.db.executeSql("INSERT INTO data (dId , sId , subId , subName ," +
" dDate , cId , cName , stdId , stdName ) VALUES (?, ?,?, ?,?, ?,?, ?,?)",
[dId, sId, subId, subName, dDate, cId, cName, stdId, stdName]).then((data) => {
resolve(data);
}, (error) => {
reject(error.tostring());
});
});
}
}
数据库插入和调用
for (let temp of ApiData) {
this.DbHandler.IsAvailable(temp.dId).then(data => {
if (data) {
console.log("did Available editing " + data);
this.DbHandler.editData(temp.dId, temp.sId, temp.subId, temp.subName,
temp.dDate, temp.cId, temp.cName);
} else {
console.log("did not Available inserting " + data);
this.DbHandler.addData(temp.dId, temp.sId, temp.subId, temp.subName,
temp.dDate, temp.cId, temp.cName);
}
});
}
this.getDataFromDb();
在开始 SELECT 请求之前,我想完成多个 INSERT.我的问题是 SELECT 触发时 INSERT 尚未完成.
I have multiple INSERTs I'd like to be done before starting SELECT requests. My problem is that the INSERT is not yet finished when the SELECT fires.
推荐答案
在then
方法中调用read from DB:
Call read from DB in then
method:
let promises = [];
for (let temp of ApiData) {
let promise = new Promise((resolve, reject) => {
this.DbHandler.IsAvailable(temp.dId).then(data => {
if (data) {
console.log("did Available editing " + data);
this.DbHandler.editData(temp.dId, temp.sId, temp.subId, temp.subName,
temp.dDate, temp.cId, temp.cName).then(() => resolve());
} else {
console.log("did not Available inserting " + data);
this.DbHandler.addData(temp.dId, temp.sId, temp.subId, temp.subName,
temp.dDate, temp.cId, temp.cName).then(() => resolve());
}
});
};
promises.push(promise);
}
Promise.all(promises).then(() => this.getDataFromDb());
这篇关于如何在 ionic 2/3 中处理数据库异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 ionic 2/3 中处理数据库异步操作
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01