Cordova: Best way to store persistent data on client-side?


#1

I want to add a teaser slider to the start page of my mobile app. This slider should show up only once per device, so I want to store that the user has seen it locally. What’s the best-practice approach for storing persistent data on mobile devices? Would it be HTML5 local storage, or are there other options? Maybe even packages?

EDIT: Ideally, the solution should also survive any app updates from the app-store.


My Meteor app magically has offline capabilities
#2

Localstorage should be fine, but you could also use the file api but I guess that would be overkill, although perhaps the safest in terms of app update survival.


#3

Just saw on Caniuse that Mobile Safari only supports localStorage since iOS 8: http://caniuse.com/#search=localstorage If that’s correct, it won’t work on iOS 7. Would not be good if the user couldn’t get rid of the intro teaser due to this issue on older iOS devices.


#4

Hold on, is your mobile app a cordova app? In that case you’ll be bundling your own webview component and if the cordova version provides you the api, you should be good to go. The same goes for the file api which is not supported by iOS 7 either.


#5

Thanks for your quick reply. What do you mean by that statement exactly - I always thought that Cordova would use the default WebView components? Unless you exchange them explicitly, like Crosswalk does.


#6

Sorry you are right. I was assuming just that and in fact Meteor 1.3 makes great improvement in that area.

Granted, it may sometimes make the app too large, an exceptionally big hurdle with android.


#7

You could always try localForage. Potentially overkill for just storing one item but it’s a good fit for the topic title.


#8

a good option is the ground:db package


#9

@oliverlloyd @norBu Thanks for your tips! As I see it, localForage seems to fit well for my use case, but gound:db is also very interesting if you also want to sync data with the server (which is not required here, or even counter-productive). But it’s good to know, I will definitely use this, too, for other purposes!

The only thing I am still wondering is if the localForage data really survives an app update, and how it behaves on iOS7 devices. Internally, it uses many different approaches, but I found no info which one would work in mobile scenarios.

EDIT: I just saw on Github that the localForage package hasn’t been updated for 2 years. It seems as if raix is not really developing it further, its version is on 0.0.0.


#10

This cordova plugin lets you set and manage cookies in your cordova app

It may fit your use case, not sure it’d survive app updates though


#11

@waldgeist: I think local storage is probably the way to go. Not sure how important iOS 7 support is for your app, but the version of Cordova we upgraded to for Meteor 1.3 requires iOS 8 or higher, so that might be another consideration.


#12

It looks like it’s not up to date because the latest version is in a different branch but the default is still set to master on the repo. I think Martijn is right though, local storage would be the simplest solution.


#13

Do you know if localStorage persists through app updates (hot code and app store updates)?


#14

localStorage should persist through app updates, but it seems data might sometimes be removed when space on the device is low.

For simple values, an alternative might be to use a platform-specific preference mechanism. Using [this plugin]
(https://github.com/apla/me.apla.cordova.app-preferences) seems promising for that.


#15

Thanks, this plugin really seems promising!


#16

This package looks perfect for storing preference style data, which is what I was looking for. I’ll give it a shot and report back.


#17

Installing app-preferences gives me the following error while building.

Errors executing Cordova commands:         
                                              
   While preparing Cordova project for platform Android:
   Error: Cannot find module 'cordova'        
   at Function.Module._resolveFilename (module.js:338:15)
   at Function.Module._load (module.js:280:25)
   at Module.require (module.js:364:17)
   at require (module.js:380:17)
   at Context.requireCordovaModule

I installed it with meteor add cordova:cordova:cordova-plugin-app-preferences@0.7.7.

Any ideas?


#18

Guess this should be reported to the plugin repo issues.


#19

Sorted that issue with ‘meteor reset’ somehow, but installing the app-preferences cordova plugin results in an error which seems to be caused by using an older version of node. https://github.com/apla/me.apla.cordova.app-preferences/issues/78

Failed to install 'cordova-plugin-app-preferences':TypeError: Uh oh!
Object # has no method 'parse'

Is there anyway around this?


#20

@npolet Hi im facing a similar issue while trying to add this plugin to my project…what were your steps if you finally figured it out?
here is my error log
`Started proxy.
=> Started MongoDB.
% Failed to install ‘cordova-plugin-app-preferences’:Error: Cannot find module 'cordova’
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Context.requireCordovaModule (/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/cordova-lib/src/hooks/Context.js:77:12)
at module.exports (/Users/zehijeanjacquesthibaut/Desktop/TRIZ_DIGITAL/WEBAPPS_MOBILE_APPS/NSIA/AgenceNumerique/.meteor/local/cordova-build/plugins/cordova-plugin-app-preferences/bin/lib/android.js:11:13)
at module.exports (/Users/zehijeanjacquesthibaut/Desktop/TRIZ_DIGITAL/WEBAPPS_MOBILE_APPS/NSIA/AgenceNumerique/.meteor/local/cordova-build/plugins/cordova-plugin-app-preferences/bin/after_plugin_install.js:9:37)
at runScriptViaModuleLoader (/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/cordova-lib/src/hooks/HooksRunner.js:167:18)
at runScript (/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/cordova-lib/src/hooks/HooksRunner.js:145:16)
at /Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/cordova-lib/src/hooks/HooksRunner.js:130:20
at _fulfilled (/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:787:54)
at self.promiseDispatch.done (/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:816:30)
at Promise.promise.promiseDispatch (/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:749:13)
at /Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:810:14
at flush (/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:108:17)
at process._tickCallback (node.js:458:13)
=> Errors executing Cordova commands:

While adding plugin https://github.com/apla/me.apla.cordova.app-preferences.git#60e0c1bdcf9129ba347a1fadbac7cb6eaa092e64 to Cordova project:
Error: Cannot find module 'cordova’
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Context.requireCordovaModule
(/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/cordova-lib/src/hooks/Context.js:77:12)
at module.exports
(/Users/zehijeanjacquesthibaut/Desktop/TRIZ_DIGITAL/WEBAPPS_MOBILE_APPS/NSIA/AgenceNumerique/.meteor/local/cordova-build/plugins/cordova-plugin-app-preferences/bin/lib/android.js:11:13)
at module.exports
(/Users/zehijeanjacquesthibaut/Desktop/TRIZ_DIGITAL/WEBAPPS_MOBILE_APPS/NSIA/AgenceNumerique/.meteor/local/cordova-build/plugins/cordova-plugin-app-preferences/bin/after_plugin_install.js:9:37)
at runScriptViaModuleLoader
(/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/cordova-lib/src/hooks/HooksRunner.js:167:18)
at runScript
(/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/cordova-lib/src/hooks/HooksRunner.js:145:16)
at
/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/cordova-lib/src/hooks/HooksRunner.js:130:20
at _fulfilled
(/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:787:54)
at self.promiseDispatch.done
(/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:816:30)
at Promise.promise.promiseDispatch
(/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:749:13)
at
/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:810:14
at flush
(/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/lib/node_modules/q/q.js:108:17)
at process._tickCallback (node.js:458:13)

/Users/zehijeanjacquesthibaut/.meteor/packages/meteor-tool/.1.3.4_1.1ff5caj++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/isopackets/cordova-support/npm/node_modules/meteor/promise/node_modules/meteor-promise/promise_server.js:165
throw error;
^
ExitWithCode:1
`