In this post, we will add Cordova’s SQLite storage plugin in Ionic version 4 Application. This is the common and widely used storage plugin by most of the Ionic developers since version 1 of Ionic. Storage is widely used to store personalized settings by the user in the application which is not required to be stored on the server side.
Application developers sometimes need to save a bunch of information on the user side to control some features and provide some offline functionalities to application users, in that case, local storage proves very handy. Using Ionic’s Native storage plugin we can storage key value pairs which may have any type of data from a simple string or integers to JSON objects. But this locally stored data will remain until the application is Uninstalled or device is formatted.
LocalStorage engines are managed by the plugin itself according to OS on which application is installed or running like in Native app context Storage will be done using SQLite, which is one of the most stable and most preferred file-based storage databases. On Progressive Web Apps platform IndevedDB, WebSQL or localStorage is chosen in a defined order.
Also, Check:
Let’s start the implementation of Storage Plugin in Ionic 4 Application
Here we will start with a new application in Ionic version 4 which is now in beta phase but soon stable version will be available with minor installation step changes in commands to install.
Create A New Ionic 4 Application
Create a new application using Ionic CLI
To create an application you must have NodeJS and Ionic Cordova CLI
To create an application you must have NodeJS and Ionic Cordova CLI
Run the following command to create a new application
Install Cordova And Ionic Native Plugin
Import Declare Plugin In Application’s Module File
In the app.module.ts file, import plugin then adds in Imports array in NgModule. Replace below code:
After adding the plugin to the app’s main module, we can now use Native Storage anywhere in the application. Here we will use it in the home component.
In the home.page.ts file, import plugin then creates an instance in component’s constructor.
In the above code, we just simply setting and getting the string value.
Set A Value In A Key
set() : Set the value for the given key. Returns a promise that resolves when the key and value are set
Get Value In A Key
get() : Get the value associated with the given key. Returns a promise with the value of the given key
Remove A Key/Value Pair
remove() : Remove any value associated with this key. Returns a promise that resolves when the value is removed
Current Storage Engine Used
Get the name of the driver being used.
Traverse Key/Value Pairs
forEach() : Iterate through each key,value pair. Returns a promise that resolves when the iteration has finished.
All Stored Keys
keys() : Returns a promise that resolves with the keys in the store.
Total Keys Stored
length() : Returns a promise that resolves with the number of keys stored.
You can check more options here
Finally out component will look like this
To call these methods, add below HTML in home.page.html
Here we discussed How we can Integrate Cordova’ Storage Native plugin in Ionic 4 Application to Store Values in different formats and retrieve them as per application requirements.
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.