We have to deal with data storage when we start building real-time apps. The mobile environment allows us to store data in two ways. i.e., on a different server or locally on our device. The server on which the data is stored is based on the type of data, volume, and requirements. For instance, if we want to store a little bit of data required frequently, it would be best to store it on our mobile device. The server side storing of data is good when we have a large volume of data to store, retrieve, and update.
If we know HTML5, we can either use the localStorage object or sessionStorage object. We can use these object for data storage purposes in PhoneGap because it uses HTML5. The localStorage object is used to store data permanently on our mobile device, and the sessionStorage object is used to store data for a particular session on our mobile device. Apart from these two ways, we can also store data on the server.
The localStorage object stores the data permanently on our device without an expiry. The data stored on the mobile device will be available even if we close our application and try to get it even six months after. The data is stored in key/value pairs in localStorage. The user experience gets improved significantly when we store data using the localStorage object. This is because the required data can be fetched so fast without waiting for network connectivity or any other related issues.
The localStorage object provides three methods to work with data, i.e., setItem, getItem, and removeItem.
We will write it in the following way:
Like the localStorage object, the sessionStorage object also provides three methods to deal with data, i.e., setItem, getItem, and removeItem. All these three methods work in the same way as they work in the localStorage object. For sessionStorage, we will write it in the following way: