Gavishiddappa Gadagi
Gavishiddappa Gadagi

Reputation: 1180

localStorage and Storage in ionic 2 which is better?

I wanted store my authentication code in localstorage, ionic2 provides it's own storage module ionic/storage

storage.set(key,value),
storage.get(key).then(value=> {
   console.log(value);
})

but I also tried storing like this and it works when i tested the app in browser.

localStorage.setItem(key, value);
localStorage.getItem('key');

can anybody explain me which is better way to use local storage and why ?

Upvotes: 2

Views: 759

Answers (1)

sebaferreras
sebaferreras

Reputation: 44669

Ionic's Storage is definitely better. As you can see in the docs:

Storage is an easy way to store key/value pairs and JSON objects. Storage uses a variety of storage engines underneath, picking the best one available depending on the platform.

When running in a native app context, Storage will prioritize using SQLite, as it's one of the most stable and widely used file-based databases, and avoids some of the pitfalls of things like localstorage and IndexedDB, such as the OS deciding to clear out such data in low disk-space situations.

When running in the web or as a Progressive Web App, Storage will attempt to use IndexedDB, WebSQL, and localstorage, in that order.

So basically, Ionic's storage will try to use the best available option, without you to even worry about that. In the docs you can also find how to install SQLite in your project, which would allow Ionic's Storage to use it, and would be the best way to store information in your app.

Upvotes: 5

Related Questions