database - 使用 asyncstorage 存储在移动设备中的数据在 Internet 开启时移动到数据库
问题描述
用户离线时使用异步存储在移动设备中存储的数据。当用户在线时,必须将相同的数据存储在数据库中。
这将检查互联网是否打开。如果连接,它将保存到手机,否则将存储到数据库。handleFirstConnectivityChange = isConnected => { NetInfo.isConnected.removeEventListener( "connectionChange", this.handleFirstConnectivityChange );
if (isConnected === false) {
Alert.alert("You are offline!");
this.saveKey(
this.state.projid,
this.state.client,
this.state.contractor,
this.state.title,
this.state.location,
this.state.desc,
this.state.unit,
this.state.drill,
this.state.logger
);
} else {
Alert.alert("You are online!");
this.sync();
}
};
这是将数据保存到手机的功能
saveKey(
projid,
client,
contractor,
title,
location,
desc,
unit,
drill,
logger)
{
if((this.state.projid)&&(this.state.client)&&(this.state.contractor)&&(this.state.title)&&(this.state.location)&&(this.state.desc)&&(this.state.unit)&&(this.state.drill)&&(this.state.logger)){
let storedObject = {};
storedObject.field1 = projid;
storedObject.field2 = client;
storedObject.field3 = contractor;
storedObject.field4 = title;
storedObject.field5 = location;
storedObject.field6 = desc;
storedObject.field7 = unit;
storedObject.field8 = drill;
storedObject.field9 = logger;
try {
AsyncStorage.setItem('projectInfo', JSON.stringify(storedObject));
console.log("text1 in setitem and text2 is" + JSON.stringify(storedObject));
Alert.alert('Data Saved');
} catch (error) {
}
}
else {
Alert.alert('Please fill data');
}
}
此函数将数据插入数据库
sync() {
console.log('inside sync function');
console.log(this.state.location);
const { projid } = this.state;
const { client } = this.state;
const { contractor } = this.state;
const { title } = this.state;
const { location } = this.state;
const { desc } = this.state;
const { unit } = this.state;
const { drill } = this.state;
const { logger } = this.state;
//alert(user_name, user_contact, user_address);
if (projid) {
if (client) {
if (contractor) {
if (title) {
if (location) {
if (desc) {
if (unit) {
if (drill) {
if (logger) {
db.transaction(function(tx) {
tx.executeSql(
'INSERT INTO projInfo (projectId,client,contractor,title,location,description,unit,drillers,loggers) VALUES (?,?,?,?,?,?,?,?,?)',
[projid, client, contractor, title,location,desc,unit,drill,logger],
(tx, results) => {
console.log('Results', results.rowsAffected);
if (results.rowsAffected > 0) {
Alert.alert(
'Success',
'Inserted Successfully',
[
{
text: 'Ok'
},
],
{ cancelable: false }
);
} else {
alert('Insert Failed');
}
}
);
});
} else {
alert('Please fill loggers');
}
} else {
alert('Please fill drillers');
}
}else {
alert('Please fill unit');
}
} else {
alert('Please fill description');
}
} else {
alert('Please fill location');
}
} else {
alert('Please fill title');
}
} else {
alert('Please fill contractor');
}
} else {
alert('Please fill client');
}
} else {
alert('Please fill project ID');
}
}
解决方案
您可以使用react-native-net-info
. 这是文档的链接:https://github.com/react-native-community/react-native-netinfo#addeventlistener
import NetInfo from "@react-native-community/netinfo";
....
componentDidMount() {
NetInfo.addEventListener(state => {
if (state.isConnected) {
//Do whatever you want here.
}
});
}
....
推荐阅读
- mysql - 我想编写一个 mySQL SELECT 查询来消除在一列中具有重复值的行
- image - 从三元轮廓图像文件中提取数据
- javascript - 类型错误:chrome.tabs.executeScript 不是函数
- bash - 从包含 pdf 路径的列表文件中打开 pdf
- r - R - 将列拆分为三列并将它们添加为新列
- google-analytics - 如何在不使用会员区的情况下跟踪 Squarespace 网站上的个人用户活动?
- android - Ionic5 电容器修复 AndroidManifest.xml 缺少活动名称
- bash - 在 PhpStorm 中重新加载终端/Git Bash
- javascript - 如何使用滑块 Material UI ReactJS 中的键存储新的更新值
- javascript - 在 Android Chrome 中查看下载的页面时,按钮不再起作用。(javascript没有运行)