首页 > 解决方案 > 使用 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');
            }
        }

标签: databasesqlitereact-nativesynchronizationasyncstorage

解决方案


您可以使用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.
      }
    });
  }
....

推荐阅读