Versions: Cordova: 10.0.0 Cordova-Android: 9.0.0
Goal: Items saved to the LocalStorage should be persisted immediately and across whatever happens.
Investigation: I first came across this issue when I left the app running over the weekend. When I came back the phone's battery was empty and after starting the app some data of the LocalStorage was lost.
So I did some tests and first I couldn't really reproduce the issue. What I tried:
Writing data into LocalStorage (localStorage.setItem('foo', 'bar'))
Then I actually managed reproducing the issue:
localStorage.setItem('foo', 'bar'))localStorage.getItem('foo') => undefinedI ran the next test:
localStorage.setItem('foo', 'bar')
localStorage.setItem('foo2', 'bar2')localStorage.getItem('foo') => 'bar'
localStorage.getItem('foo2') => undefinedSo it seems as the last stored item gets lost. Which doesn't make too much sense to me.
I tried to figure out where the Android Webview persists the LocalStorage to, to verify if the data is persisted to the file system after a setItem call.
The best thing I could find was this command to access the LocalStorage Log file.
adb exec-out run-as com.package.name cat /data/data/com.package.name/app_webview/Default/Local\ Storage/leveldb/000003.log
So every time I did an action like setItem, or after removing the battery I checked the LocalStorage log file directly on the device.
localStorage.setItem('foo', 'bar') => file://...foo...bar was added to the log file.file://...foo...bar was not present any more in the log file.Summary: LocalStorage loses the last set item when removing the battery while the app is still running.
My current fix would be to always call an additional setItem with the current timestamp, but I really would like to find a proper solution for that.
Questions: How can I check the current persisted LocalStorage Object in the Android file system? Why is this even happening and how do I best fix it?