Saving Data With Ionic Framework

Coming from native Android and SQLite, the concept of universal data storage on local devices has been different. Apache Cordova and Ionic Framework, being as awesome as they are, support HTML5 local storage calls. This allows us to store and retrieve data without the use or knowledge of SQL and on any platform we plan to use.

Typically you would make use of the local storage by using commands like the following:

The above is an example of an fictional API request that gets a users profile. When the GET request succeeds, the result is stored in the scope as well as a local storage bucket. However, you can’t just store the raw object into local storage. You must first serialize it with the JSON.stringify command. This will create a flat string instead of an object. Now lets say the profile API is called a second time, but the mobile device’s internet isn’t stable or it doesn’t exist. The error callback will happen and if the local profile bucket exists, JSON.parse will un-flatten it and store it in our scope. Besides seeing possibly older data, the user won’t even know that there was a problem.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.