Data Storage
An introduction to Ionic's built-in data storage utility, Ionic Storage.
We'll cover the following
Ionic Storage
Let’s face it, without some way of persisting data, our apps are going to be pretty useless in the long run.
Whether we want to load and display a list of user scores for previous sessions of a game or allow users to manage personal data, the ability to both store and retrieve data is hugely important.
We’ll explore how to persist data across different app sessions using Ionic’s default built-in storage utility, the creatively named Ionic Storage.
Why Ionic Storage?
As an open-source data storage utility, Ionic Storage provides developers with a way to store key or value pairs and JSON objects with an intuitive and easy-to-use API similar to that offered by HTML 5 LocalStorage.
Acting as an abstraction layer, Ionic Storage allows developers to manage their data while the underlying storage mechanism of choice is determined by the platform environment that the application is running on.
For example, SQLite will be prioritized as the default storage mechanism if Ionic Storage is used in a native app context (iOS or Android).
If, however, an application is running on the web or as a PWA, then Ionic Storage will attempt to utilize the following storage APIs in this order:
- IndexedDB
- WebSQL
- LocalStorage
As the choice of storage engine is managed in the background by Ionic Storage, we simply interact with application data via the following API methods (where required):
- The
clear()
method - The
forEach()
method - The
get()
method - The
keys()
method - The
length()
method - The
create()
method - The
remove()
method - The
set()
method
As stated earlier, this is very similar to the HTML5 LocalStorage API and provides a simple interface for developers to use when managing data in their applications.
Unsurprisingly, we can start implementing this API quite quickly in our projects.
Another Ionic alternative
If a more enterprise-level or secure solution is needed, Ionic’s Offline Storage would be a better option for those applications requiring data encryption, greater security (thanks to filesystem storage), and more robust data management and performance courtesy of an underlying industry-standard SQL engine.
Be aware, though, that unless our application requires such a solution, the default Ionic Storage utility should be sufficient to meet our needs.
Get hands-on with 1200+ tech skills courses.