Don’t think that LocalStorage is reliable in the Hybrid app.

Don’t think that localStorage is reliable in hybrid app

Note: This article has been translated. If there are any errors, please correct me. You can also move to the original text.

There are many ways to store data locally in the Cordova/PhoneGap app, and the most commonly used is undoubtedly localStorage. LocalStorage provides a solution for storing key-value pair data:

// simple examplelocalStorage.setItem('name', 'Pinky'); var name = localStorage.getItem('name'); console.log(name);// complex examplevar records = [{ name: 'Pinky', age: 1 }, {name:  'Brain', age: 2 }]; localStorage.setItem('records', JSON.stringify(records)); var output = JSON .parse(localStorage.getItem('records')); console.table(o utput);

Although localStorage is much more powerful than Cookie storage, when using localStorage in Cordova/PhoneGap app, Android and IOS also have up to 5MB of space.

In most cases, even if you close the app or turn off the phone, the data stored using localStorage can continue to exist, but the processing of localStorage on Android and IOS devices may cause problems:

IOS

IOS 8, when the system memory is insufficient, localStorage may be cleared, just like this post in the ionic forum said.

Android

Also in the ionic forum, many users encountered the situation of localStorage being cleared, just like this post.

So, can’t you use the powerful and simple localStorage?
Of course, you can’t use it, but you should use it properly: you should use localStorage to store data that does not need to be permanently saved. A good scenario for using localStorage is to use localStorage to store data obtained from the outside. Even if localStorage is cleared, it can be obtained again by requesting external data.

Alternative plan

In hybrid app, if you need to take into account the experience and persistent storage, you can use SQLite database. One solution is to use PouchDB, an open source JavaScript framework, PouchDB encapsulates WebSQL, IndexedDB and SQLite. If you want to know how to use PouchDB, you can check my other translation. Another solution is to use LokiJS. LokiJS is a fast in-memory database. If you want to know how to use LokiJS, you can check my other translation ionic how to use LokiJS as local storage.

Recommend a personal wealth management product that has been used for half a year: After 6 years of digging, the newcomer’s income is 36%, 7 days 18%, 1 year 10%, registration is free of 308 yuan coupons

Don’t think that localStorage is reliable in hybrid app

Note: This article has been translated. If there are any errors, please correct me. You can also move to the original text.

There are many ways to store data locally in the Cordova/PhoneGap app, and the most commonly used is undoubtedly localStorage. LocalStorage provides a solution for storing key-value pair data:

// simple examplelocalStorage.setItem('name', 'Pinky'); var name = localStorage.getItem('name'); console.log(name);// complex examplevar records = [{ name: 'Pinky', age: 1 }, {name:  'Brain', age: 2 }]; localStorage.setItem('records', JSON.stringify(records)); var output = JSON .parse(localStorage.getItem('records')); console.table(out put);

Although localStorage is much more powerful than Cookie storage, when using localStorage in Cordova/PhoneGap app, Android and IOS also have up to 5MB of space.

In most cases, even if you close the app or turn off the phone, the data stored using localStorage can continue to exist, but the processing of localStorage on Android and IOS devices may cause problems:

IOS

IOS 8, when the system memory is insufficient, localStorage may be cleared, just like this post in the ionic forum said.

Android

Also in the ionic forum, many users encountered the situation of localStorage being cleared, just like this post.

So, can’t you use the powerful and simple localStorage?
Of course, you can’t use it, but you should use it properly: you should use localStorage to store data that does not need to be permanently saved. A good scenario for using localStorage is to use localStorage to store data obtained from the outside. Even if localStorage is cleared, it can be obtained again by requesting external data.

Alternative plan

In hybrid app, if you need to take into account the experience and persistent storage, you can use SQLite database. One solution is to use PouchDB, an open source JavaScript framework, PouchDB encapsulates WebSQL, IndexedDB and SQLite. If you want to know how to use PouchDB, you can check my other translation. Another solution is to use LokiJS. LokiJS is a fast in-memory database. If you want to know how to use LokiJS, you can check my other translation ionic how to use LokiJS as local storage.

Recommend a personal wealth management product that has been used for half a year: After 6 years of digging, the newcomer’s income is 36%, 7 days 18%, 1 year 10%, registration is free of 308 yuan coupons

Leave a Comment

Your email address will not be published.