无网络时-将数据保存到sqlite,并在网络恢复后发送到服务器
这是离子2+-I在加载时检索对象数组。当网络存在并且用户执行操作时,该对象itemID随后被发送到该API端点。当没有任何网络时,itemID应该保存到SQLite数据库,然后在网络恢复后推送到API端点 组成部分 正在保存要保存到db的初始对象数组无网络时-将数据保存到sqlite,并在网络恢复后发送到服务器,sqlite,cordova,ionic-framework,ionic2,ionic3,Sqlite,Cordova,Ionic Framework,Ionic2,Ionic3,这是离子2+-I在加载时检索对象数组。当网络存在并且用户执行操作时,该对象itemID随后被发送到该API端点。当没有任何网络时,itemID应该保存到SQLite数据库,然后在网络恢复后推送到API端点 组成部分 正在保存要保存到db的初始对象数组 private createDB(): void{ this.sqlite.create({ name: 'DATABASE_FILE_NAME', location: 'default'
private createDB(): void{
this.sqlite.create({
name: 'DATABASE_FILE_NAME',
location: 'default'
}).then((db: SQLiteObject) => {
this.db = db;
this.createTables();
})
.catch(e => console.log(e));
}
private createTables(): void{
this.db.executeSql('CREATE TABLE IF NOT EXISTS "ITEMS" ( `rowid` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `something` BOOL, `something` BOOL, `itemID` INT )', {}).then((res) => { console.log(res, 'Executed SQL')
}).catch(e => console.log(e));
}
我正在检索数组
[
{itemID: 10, something: "blue", something: "1"}
{itemID: 11, something: "red", something: "2"}
{itemID: 12, something: "green", something: "2"}
{itemID: 13, something: "yellow", something: "1"}
]
向服务器发送操作函数
this.service.sendAction(itemID).subscribe(result => {})
即使存在网络连接,也可以将数据保存到SQLite,然后将其发送到API。在这种情况下,从第一步开始,您只能在
else
中使用statemen
下面是您正在寻找的场景
1.)将数据保存到SQLite(page.ts)
2.)下一步可以在仪表板或主页上进行,在那个里,当网络存在时,您将检查网络连接并将数据从SQLite发送到API。(dashboard.ts)
将数据从SQLite发送到API的函数
sendData(){
this.count = 0;
this._database.getLocalData().then((result) => {
this.DataList = <Array<Object>> result;
if(this.DataList.length !== 0){
this.DataList.forEach(function(item) {
this.service.sendAction(item).subscribe(result => {
if(res.status == "ok" && this.count == this.DataList.length-1){
//empty SQLite local table if success
this._database.deleteLocalData();
}
})
}
} else {
//
}
}, (error) => {
console.log("Offline data not sent!",
error);
});
}
sendData(){
此值为0.count;
这是._database.getLocalData()。然后((结果)=>{
this.DataList=结果;
if(this.DataList.length!==0){
this.DataList.forEach(函数(项){
this.service.sendAction(项目).subscribe(结果=>{
如果(res.status==“ok”&&this.count==this.DataList.length-1){
//如果成功,则清空SQLite本地表
这是._database.deleteLocalData();
}
})
}
}否则{
//
}
},(错误)=>{
console.log(“未发送脱机数据!”,
误差);
});
}
下面是存储数据的SQLite表的示例。(数据库服务.ts)
public getLocalData(){
返回新承诺((解决、拒绝)=>{
this.sqlite.create({
名称:“dbName.db”,
位置:“默认”
})
.然后((db:SQLiteObject)=>{
db.executeSql(“从表名中选择*,[])。然后((数据)=>{
让DataList=[];
如果(data.rows.length>0){
for(设i=0;i{
拒绝(错误);
});
})
.catch(e=>console.log(e));
});
}
如果表tableName未创建,我将如何获取它?我应该早点问这个问题。或者我会从我的问题开始创建表,并使用您在上面提供的示例吗?@userlkjsflkdsvm您应该首先创建表。我通常对所有表使用单独的服务/提供者。请参阅我遇到的问题示例是,我只想保存一个对象,而不是数组。就像在我的示例中一样,我只向该api发送一个对象,而不是对象数组。在上面的示例中,您保存的对象是“this.\u databaseService.sendAction(object).then(result=>{})”,但如果您不希望数组,则将数据从SQLite发送到api“sendData()”“DataList”。在这种情况下,如果不再有网络连接,您就不能将多个对象保存到SQLite数据库。我猜这就是问题所在,使用您的示例,它希望我发送一个数组而不是对象。我同意您的看法,我确实希望保存一个数组,以便可以存储更多对象。
ionViewDidEnter(){
this.sendData();
}
sendData(){
this.count = 0;
this._database.getLocalData().then((result) => {
this.DataList = <Array<Object>> result;
if(this.DataList.length !== 0){
this.DataList.forEach(function(item) {
this.service.sendAction(item).subscribe(result => {
if(res.status == "ok" && this.count == this.DataList.length-1){
//empty SQLite local table if success
this._database.deleteLocalData();
}
})
}
} else {
//
}
}, (error) => {
console.log("Offline data not sent!",
error);
});
}
public getLocalData() {
return new Promise((resolve, reject) => {
this.sqlite.create({
name: 'dbName.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql("SELECT * FROM tableName", []).then((data) => {
let DataList = [];
if(data.rows.length > 0) {
for(let i = 0; i < data.rows.length; i++) {
DataList.push({
itemID: data.rows.item(i).itemID,
something: data.rows.item(i).something
});
}
}
resolve(DataList);
}, (error) => {
reject(error);
});
})
.catch(e => console.log(e));
});
}