ngrx-store-ionic-storage
ngrx-store-ionic-storage copied to clipboard
State gets overwritten during hydration on device
This may be similar (or even the same)? as this issue
When running my application in browser (ionic serve), all works well. I see the state stored in local storage, and it always reinstatted at startup.
However, when running on a devie (eg Android phone), when I restart the app (after closing it), the state was always empty.
I added some logging into the packages index,js
save and fetch methods...
function fetchState() {
return storage
.get(STORAGE_KEY)
.then(function (s) {
console.log("STORE: got data" + JSON.stringify(s));
return s || {};
})
.catch(function (err) {
console.log("STORE: ERROR" + err);
});
}
function saveState(state, keys) {
// Pull out the portion of the state to save.
if (keys) {
state = keys.reduce(function (acc, k) {
var val = getNested(state, k);
if (val) {
setNested(acc, k, val);
}
return acc;
}, {});
}
// Look at state before writing
console.log(" ----------- EXAMINE STATE before saving ----------");
fetchState().then(s => {
console.log("STATE before saving...: " + JSON.stringify(s));
console.log("----- Saving STATE: ------- " + JSON.stringify(state));
return storage.set(STORAGE_KEY, state);
});
var p = new Promise(resolve => {
resolve();
});
return p;
}
Now restarting the app, and looking at the console output (attaching via chrome://inspect), I could see it seems to be saving out an initial state before getting it...
From console.log I can see the first lines...
Ionic Native: deviceready event fired after 0 ms
vendor-es2015.js:124230 @ngrx/store: runtime checks are currently opt-in but will be the default in the next major version with the possibility to opt-out, see https://ngrx.io/guide/migration/v8 for more information.
createActiveRuntimeChecks @ vendor-es2015.js:124230
2vendor-es2015.js:170566 ----------- EXAMINE STATE before saving ---------
So this ----------- EXAMINE STATE before saving ---------` is where I log out what state is store, just before it saves it.
I see the full state that has been saved, but then where I see ----- Saving STATE: -------
it is being passed an empty state. ''Seems to be doing this a few times, and I soon see the the state saved out as empty.
I wonder if the problem is because of the async nature of Ionic storage, and that fact that reducers are synchronous. An issue about this here