I added the ionic storage 1.1.6
to my app to store user login info in one single field. Hence, after login the app sets and at next login gets the user.
However, this circle works once or twice after fresh App restart but then get fails to get a field on the iOS
device (it’s empty) and forwards to my login screen again.
What default engine is used in storage? Mind you I have the default version of storage - v1.1.6
installed and not the one described on the tutorial page, which seems to be 1.1.7
where you can select the storage engine. I think I will try sqlite
for now. Since I am porting my app from io1
to io2
, I used localstorage
previously and that worked flawlessly (except random localstorage wipes by the os of course)
return Observable.fromPromise(
this._storage.get('user').then( userString => {
//returns user object or null when user is not set.
this.userInfo = JSON.parse(userString);
return this.userInfo;
})
);
let userString = '{"email":"'+email+'","password":"'+password+'"}';
try {
JSON.parse(userString);
this._storage.set('user', userString);
return true;
}catch(err){
console.log('Error adding user - JSON was corrupted. Abort');
return false;
}
Any chance you’re hitting a race condition whereby sometimes the async read from storage has completed before you try to use it and sometimes it hasn’t?
Yes, something like that. I think the storage plugin is not initiated when the first access to the plugin is called. Hence I am getting null.
Is there a way to wait for that event?
I found Storage.ready
in the storage source-code, but it is available for 1.1.7
, not for 1.1.6
as it seems. I think that should be it.
My custom console.logs
IN getLocalUser
null #result of DB request in getLocaUser
START USER CHECK
null #return value of getLocalUser
fire before the SQLitePlugin
initializes
2016-12-23 19:43:46.963796 myApp[3040:745035] Apache Cordova native platform version 4.3.1 is starting.
2016-12-23 19:43:46.964964 myApp[3040:745035] Multi-tasking -> Device: YES, App: YES
2016-12-23 19:43:47.280166 myApp[3040:745035] Using UIWebView
2016-12-23 19:43:47.285011 myApp[3040:745035] [CDVTimer][handleopenurl] 0.108004ms
2016-12-23 19:43:47.290622 myApp[3040:745035] [CDVTimer][intentandnavigationfilter] 5.473971ms
2016-12-23 19:43:47.290854 myApp[3040:745035] [CDVTimer][gesturehandler] 0.120997ms
2016-12-23 19:43:47.347840 myApp[3040:745035] [CDVTimer][splashscreen] 56.824028ms
2016-12-23 19:43:47.368710 myApp[3040:745035] [CDVTimer][statusbar] 20.656943ms
2016-12-23 19:43:47.372431 myApp[3040:745035] [CDVTimer][keyboard] 3.542006ms
2016-12-23 19:43:47.404523 myApp[3040:745035] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2016-12-23 19:43:47.411265 myApp[3040:745035] [MC] Filtering mail sheet accounts for bundle ID: de.hrabe.myApp2, source account management: 1
2016-12-23 19:43:47.576732 myApp[3040:745035] [MC] Result: YES
2016-12-23 19:43:47.578096 myApp[3040:745035] [MC] Filtering mail sheet accounts for bundle ID: de.hrabe.myApp2, source account management: 1
2016-12-23 19:43:47.579202 myApp[3040:745035] [MC] Result: YES
2016-12-23 19:43:47.631507 myApp[3040:745035] [CDVTimer][socialsharing] 258.910000ms
2016-12-23 19:43:47.631604 myApp[3040:745035] [CDVTimer][TotalPluginStartup] 346.785009ms
2016-12-23 19:43:48.408458 myApp[3040:745035] [MC] Reading from public effective user settings.
2016-12-23 19:43:49.567279 myApp[3040:745035] Resetting plugins due to page load.
2016-12-23 19:43:51.025232 myApp[3040:745035] Finished load of: file:///var/containers/Bundle/Application/0CAB35E0-86A1-493D-A080-121B19247112/myApp.app/www/index.html
2016-12-23 19:43:51.109348 myApp[3040:745035] DEVICE READY FIRED AFTER 652 ms
2016-12-23 19:43:51.214262 myApp[3040:745035] IN getLocalUser
2016-12-23 19:43:51.214408 myApp[3040:745035] null
2016-12-23 19:43:51.214490 myApp[3040:745035] START USER CHECK
2016-12-23 19:43:51.214565 myApp[3040:745035] null
2016-12-23 19:43:51.214690 myApp[3040:745035] OPEN database: _ionicstorage
2016-12-23 19:43:51.217646 myApp[3040:745035] -[SQLitePlugin pluginInitialize] [Line 33] Initializing SQLitePlugin
2016-12-23 19:43:51.218418 myApp[3040:745035] -[SQLitePlugin pluginInitialize] [Line 44] Detected docs path: /var/mobile/Containers/Data/Application/DA5D86CC-9C9D-4824-B0CC-FA1701FA2470/Documents
2016-12-23 19:43:51.218625 myApp[3040:745035] -[SQLitePlugin pluginInitialize] [Line 48] Detected Library path: /var/mobile/Containers/Data/Application/DA5D86CC-9C9D-4824-B0CC-FA1701FA2470/Library
2016-12-23 19:43:51.218861 myApp[3040:745035] -[SQLitePlugin pluginInitialize] [Line 55] no cloud sync at path: /var/mobile/Containers/Data/Application/DA5D86CC-9C9D-4824-B0CC-FA1701FA2470/Library/LocalDatabase
2016-12-23 19:43:51.219659 myApp[3040:745035] new transaction is waiting for open operation
2016-12-23 19:43:51.219719 myApp[3040:745083] -[SQLitePlugin openNow:] [Line 137] open full db path: /var/mobile/Containers/Data/Application/DA5D86CC-9C9D-4824-B0CC-FA1701FA2470/Library/LocalDatabase/_ionicstorage
2016-12-23 19:43:51.235593 myApp[3040:745083] -[SQLitePlugin openNow:] [Line 163] Good news: SQLite is thread safe!
2016-12-23 19:43:51.300361 myApp[3040:745035] OPEN database: _ionicstorage - OK
2016-12-23 19:43:51.300500 myApp[3040:745035] DB opened: _ionicstorage
Yes, it was the race condition as you mentioned. I could resolve it with installing storage v1.1.7 and use the Storage.ready
promise.