Several storage APIs are available for Cordova applications. See html5rocks storage overview and tutorial, for a more complete overview and examples.
Each API offers advantages and disadvantages, which are summarized here. You should choose whichever best suits your needs. You can also use several different approaches within a single application for different purposes.
Local storage provides simple, synchronous key/value pair storage, and is supported by the underlying WebView implementations on all Cordova platforms.
Local storage can be accessed via
window.localStorage. The following code
snippet shows the most important methods exposed by the returned
var storage = window.localStorage; var value = storage.getItem(key); // Pass a key name to get its value. storage.setItem(key, value) // Pass a key name and its value to add or update that key. storage.removeItem(key) // Pass a key name to remove that key from storage.
For more information, see:
- Supported by all Cordova platforms.
- Its simple, synchronous API means it is easy to use.
- Only stores strings, so complex data structures have to be serialized, and only data that can be serialized can be stored.
- Performs poorly with large amounts of data. In particular:
- The lack of indexing means searches require manually iterating all data.
- Storing large or complex items is slow due to the need to serialize/de-serialize.
- Synchronous API means calls will lock up the user interface.
- Limited total amount of storage (typically around 5MB).
- iOS stores
localStoragedata in a location that may be cleaned out by the OS when space is required.
IndexedDB provides a simple and easy to understand data model, much like LocalStorage. But unlike LocalStorage, you can create multiple databases, with multiple stores per database, and its asynchronous API and search indexes provide performance benefits.
IndexedDB is supported by the underlying WebView on all platforms, with known limitations on the following platforms:
Web browser limitations
The actual behavior may depend on which browser is used. There could be differences between the behavior on the Safari and Firefox browsers, for example.
Windows platform support for IndexedDB is incomplete. For example, it lacks the following features:
- Not available in web workers.
- Doesn't support array keyPaths.
- Doesn't support array keys.
- Doesn't support object lookup via compound index.
- IndexedDB works asynchronously - you request a particular database operation, then get notified of the result via a DOM event.
- When you make a request, you get a request object, which provides
onsuccessevents, as well as properties such as
The following code snippet demonstrates some simple usage of IndexedDB:
For more information, see:
- Good performance - asynchronous API won't block the UI, and indexing provides good search performance.
- Simple data model easier to learn than SQL.
- More flexible structure than WebSQL.
- Multiple databases and object stores provides more structure than LocalStorage.
- Robustness from using a transactional database model.
- Support for versioning.
- Complex API with nested callbacks.
- Limited total amount of storage and possible eviction as described on MDN.
The FileSystem API was a W3C spec that was implemented by Chrome, but not other browsers. It provides APIs to store and retrieve data on the local file system, and is described in some detail in an excellent html5rocks article. While the API is not supported natively by any Cordova platform, the File plugin provides an extensive implementation that is available across all Cordova platforms.
The SQLite plugin provides an API virtually identical to WebSQL described above. The main differences are:
- It is available with support for the Windows platform.
- It effectively has no size limitations.
It is available in the following variations:
- cordova-sqlite-storage - core version that includes its own sqlite3 implementation. It supports iOS, Android & Windows platforms.
- cordova-sqlite-ext - extended version with additional features including REGEXP support on Android and iOS.
- cordova-sqlite-evfree - similar to cordova-sqlite-ext but with improved memory handling. Available under GPL v3 or commercial license.
Search Cordova plugins for other plugins that provide alternative storage options.