Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 data reload when network isn't present

I just want to know if this is possible and how it could be done.

Let's say an Android user has a network connection and makes a change that then updates the database. Then the user loses network connection but still makes changes on their device. They can't obviously send data to the database without a connection.

But when they receive a connection again, the changes they made when there wasn't a connection are automatically updated and sent to the database. How would I go about doing such a thing with Ionic 2 framework? Please point me in the right direction.

Thanks!

like image 688
userlkjsflkdsvm Avatar asked Jun 08 '26 16:06

userlkjsflkdsvm


1 Answers

Yes it's possible.

For that you need SQLite. It is best to make a SQLite service.


In the first phase the most important thing is to save data to offline/local database. In this case this is SQlite.

When there is no network connection and user make some action save data to table created in your SQlite database.

 finishFunction(){
   if(this._connectivityService.isOnline()){ 
     //Send data directly to external database
      this._api.sendData(this.parameters).subscribe(res => {
    }); 
   } else {
    //Store data in offline SQLite database
    this._database.myOfflineTable(this.parameters);
   }
 }

After that, in app home page check when network connection is available and call function that sends local/offline data through API to external database. When API return success message you can empty offline table.

sendMyOfflineData(){
     this._database.getOfflineTable().then((result) => {
            this.OfflineTableList = <Array<Object>> result;
            if(this.OfflineTable.length != 0){
            for(var i = 0; i < this.OfflineTable.length; i++){
              this._api.sendData(this.OfflineTable[i]).subscribe(res => {
              if(res.status == true){
                 this._database.deleteOfflineTable();
              }
         }); 
            }}
        }, (error) => {
            console.log("ERROR: ", error);
        });
   }

If you need more details, I'm here.

like image 179
Tomislav Stankovic Avatar answered Jun 11 '26 05:06

Tomislav Stankovic