I think it's working (?) but it clears every time
So after a stupid amount of time, I've got it apparently working. Using john-doherty's post I can make window.appStorage.driver() return as 'SQLiteStorage' ...after a setTimeout of 5 seconds.
I'm defining the driver as shown before I initialize my app:
localforage.defineDriver(window.cordovaSQLiteDriver).then(function() {
window.appStorage = localforage.createInstance({
version: 1.0,
size: 52428800, // 50mb
name: 'test',
storeName: 'setup',
driver: [
window.cordovaSQLiteDriver._driver, // <-- prefer cordovaSQLiteDriver
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE
]
});
if (window.appStorage.driver() !== window.cordovaSQLiteDriver._driver) {
console.warn('Not using cordovaSQLiteDriver before setItem.');
}
return appStorage.setItem('message', 'It worked!');
});
setTimeout(function(){
alert(window.appStorage.driver())
},5000);
The console.warn says it's not using SQLite, but after a 5 second timeout it says it does. However after re-opening the app, everything is cleared and I'm back to square 1. I'd hazard a guess it's maybe failing to retrieve the storage as it takes however-long to actually get set up, or by creating an instance every time it's creating a new database?
I have no idea. I've spent an enormous amount of time trying to get this to work, but I just can't see how it's going to do that.
Is this anything anyone's experienced before?
Sounds like you haven't waited till the cordovaSQLiteDriver is load. It might take some time.
I have done:
$rootScope.localForagePromiseResolve = $rootScope.localForagePromiseResolve || undefined;
$rootScope.localforageReadyPromise = $rootScope.localforageReadyPromise || new Promise(function(resolve, reject){$rootScope.localForagePromiseResolve=resolve;});
$ionicPlatform.ready(function () {
localforage.defineDriver(window.cordovaSQLiteDriver).then(function() {
return localforage.setDriver([
// Try setting cordovaSQLiteDriver if available,
window.cordovaSQLiteDriver._driver,
// otherwise use one of the default localforage drivers as a fallback.
// This should allow you to transparently do your tests in a browser
localforage.INDEXEDDB,
localforage.WEBSQL,
localforage.LOCALSTORAGE
]);
}).then(function() {
// this should alert "cordovaSQLiteDriver" when in an emulator or a device
console.log('LocalForage driver set to: '+localforage.driver());
$rootScope.localForagePromiseResolve();
})
And before every call to localforage I do:
return $rootScope.localforageReadyPromise.then(function(){
return $localForage.keys().then(function(localForageKeys){
return localForageKeys
});
});
Just one way to fix it, but it works fine and as fast as possible (no unnecessary waiting).