'Cordova Android LocalStorage loses last saved item when removing battery/running out of battery
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'))
- Force quit the app
- Restart the device while the app is running
- Playing around with airplane mode (doesn't make too much sense)
Then I actually managed reproducing the issue:
- Writing data into LocalStorage (
localStorage.setItem('foo', 'bar')) - Remove device battery while app is still running
- Start device/app and it's gone.
localStorage.getItem('foo') => undefined
I ran the next test:
- Writing data into LocalStorage:
localStorage.setItem('foo', 'bar')localStorage.setItem('foo2', 'bar2') - Remove device battery while app is still running
- Start device/app and it's partially gone:
localStorage.getItem('foo') => 'bar'localStorage.getItem('foo2') => undefined
So 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.
- After
localStorage.setItem('foo', 'bar')=>file://...foo...barwas added to the log file. - After removing the battery and restart the device =>
file://...foo...barwas 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?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
