2

I developed a hybrid app with Cordova. I used localStorage for saving some data on devices. Now I need better and persistent storage method. Also I'm looking for a synchronous library. Do you have any advice?

I just find Persisto (https://github.com/mar10/persisto). Can I use this with Cordova?

2 Answers2

1

I just find Persisto. Can I use this with Cordova?

You can use it with Cordova, but it appears to just be a wrapper around localStorage, so is no better/more persistent that using directly using localStorage.

If your Cordova app is targeting iOS then you should be aware that any locally persisted data stored inside the Webview (localStorage, WebSQL, IndexedDB) is considered cache data and therefore can be wiped at any time if the device runs low on storage space.

Also I'm looking for a synchronous library. Do you have any advice?

TL;DR: I don't think you'll find a sychronous interface to a better storage mechanism because the more durable/reliable mechanisms are all inherently asynchronous. Ultimately it would be better to bite the bullet and rework your code to work with an asynchronous storage mechanism.

For reliable persistent storage that isn't going to get wiped by iOS at a whim, you could use a native SQLite DB via the cordova-sqlite-storage plugin. For an simple interface to it, you can use a wrapper such as localForage with the cordovaSQLiteDriver adapter.

Or if your content is more file-based, you can store it using cordova-plugin-file to a durable storage location.

DaveAlden
  • 30,083
  • 11
  • 93
  • 155
  • I really do not understand why should i rework my JS to async for storing 2 lines of data. LocalForage, NativeStorage, SecureStorage etc. They are all the same.. There are dozens of async libraries. There is not even one sync and persist library. This is not rocket science. I will continue to use localStorage until Cordova ends up this nonsense and develops the synchronous method. Thanks anyway. – Vatanay Özbeyli Dec 10 '17 at 21:10
  • The interfaces to databases and the Cordova JS-native bridge are inherently asynchronous. If it was so simple as to just "develop a synchronous method" it would have been done by now. You can continue to use local storage with the caveat that iOS may wipe it at any time. – DaveAlden Dec 10 '17 at 21:17
  • Still, it's not a rocket science. Also do not worry about my data. I'm going to back-up them on Firebase when the device is online. – Vatanay Özbeyli Dec 10 '17 at 21:46
  • `Still, it's not a rocket science.` You're right: this is computer science, not the design and development of spacecraft - maybe you have confused these two? `I'm going to back-up them on Firebase when the device is online`. Good luck with that... – DaveAlden Dec 11 '17 at 08:43
0

Now I need better and persistent storage method.

You can use indexeddb for bettter persistent storage. but all the apis are asychronous and are very complex to implement.

I'm looking for a synchronous library. Do you have any advice?

You can use jsstore for executing the db code sychronously. It provides sql like apis and all the apis are sychronous. So you dont need to worry about not getting latest data etc.

So if you call update, select api one after another, then first update api will be executed and after that select api.

Again it does not make your code asychrnous, it just executed the code in the same order as you call.

Check it out here - http://jsstore.net/

if you are looking for a library to store only keypair value, then you can use KeyStore. Here you dont need to do any setup.

Check out keystore here - https://github.com/ujjwalguptaofficial/KeyStore

Ujjwal Kumar Gupta
  • 2,308
  • 1
  • 21
  • 32
  • Thank you for your answer. As far as I know, localStorage and Indexeddb are very similar. Does JSstore survive IOS app-update? – Vatanay Özbeyli Dec 11 '17 at 07:19
  • indexedb and localstorage are completely different. Check out these for more info -https://softwareengineering.stackexchange.com/questions/219953/how-is-localstorage-different-from-indexeddb, https://stackoverflow.com/questions/5924485/how-is-indexeddb-conceptually-different-from-html5-local-storage. JsStore is an indexedb wrapper, it just makes the indexedb easy to use. So it depends on the different os, whether they keep the indexeddb data or not when upgrading. You can sync with server or store the data in json file from time to time for this type of situation. Thanks. – Ujjwal Kumar Gupta Dec 11 '17 at 08:19
  • Check out official cordova docs - https://cordova.apache.org/docs/en/latest/cordova/storage/storage.html. They say that localStorage will be wiped out but there are no any info about indexeddb data wiping. See their disadvantage section. – Ujjwal Kumar Gupta Dec 11 '17 at 08:26
  • 2
    @Ujjwal Kumar Gupta I can categorically confirm that iOS wipes localStorage, IndexedDB and WebSQL data in the Webview of a Cordova app when the device runs low on space. To test this is very easy: install a Cordova app on an iDevice which contains some of this data, then fill the device up with movies until it's full. Observe that at some point "Cleaning.." appears in place of your app name on the Home screen and when you re-open your app, all your data is gone. I have been caught by this before and now store all important data in native SQLite DB which is not subject to iOS whimsical cleaning – DaveAlden Dec 11 '17 at 08:47