0

I'm making an hybrid app with crosswalk+cordova using pouchDB as storage. the problem is that when I install a new version (on android) browser indexedDB/webSQL gets completely overwritten, so all user data...is there any way to update an hybrid app on android/ios keeping userdata? thanks

user1658162
  • 2,721
  • 2
  • 19
  • 23
  • 1
    Are you simply updating the app or are you uninstalling/reinstalling? I have an app in production for nearly a year and I've never had this problem. – RamblinRose Dec 31 '16 at 14:04
  • the app is not yet in android store, so I'm reinstalling during development phase. how does update works? it's not kind of reinstall? – user1658162 Dec 31 '16 at 16:07

1 Answers1

3

IndexedDB is tied to your base URL

if you change app name or your base URL (content src) in config.xml, you will not be able to access the previous one.

I, for one, am using the localforage library and cordova sqlite plugin to smooth out browser bugs and implementation details. iOS can cause some headaches, be warned

My content entry in config.xml:

<content src="index.html" />

My relevant plugin related entries in config.xml:

<plugin name="cordova-plugin-sqlite-2" spec="~1.0.4" />
    <plugin name="cordova-plugin-crosswalk-webview" spec="~2.1.0">
        <variable name="XWALK_VERSION" value="21+" />
        <variable name="XWALK_LITEVERSION" value="xwalk_core_library_canary:17+" />
        <variable name="XWALK_COMMANDLINE" value="--disable-pull-to-refresh-effect" />
        <variable name="XWALK_MODE" value="embedded" />
        <variable name="XWALK_MULTIPLEAPK" value="false" />
    </plugin>

Here's my DB init function in the Javascript app (it returns a promise):

    function initDB() {

    let driver = [localforage.INDEXEDDB, localforage.WEBSQL]
    //force shim
    if (platform() === constants.PLATFORM_IOS) {
        if (window.sqlitePlugin) {
            //WEBSQL shim throgh sqlite2
            window.openDatabase = window.sqlitePlugin.openDatabase
        }
        driver = localforage.WEBSQL
    }


    let opts = {
        driver: driver,
        name: constants.DBName + '_forage',
        version: constants.appVersion,
        size: 4980736
    }

    localforage.config(opts)


    let stores = {
        state: localforage.createInstance({storeName: 'state'}),
        api_cache: localforage.createInstance({storeName: 'apiCache'})
    }

    const keys = Object.keys(stores)

    let p = new Promise((res, rej)=> {
        let r = 0;

        function check() {
            r++
            if (r === keys.length) {
                res(stores)
            }
        }
        keys.forEach(k=>stores[k].ready(check))

    })

    return p

}

localforage can be found here: https://github.com/localForage/localForage.git

Tudor Ilisoi
  • 2,934
  • 23
  • 25