当应用程序从内存中退出时,localStorage 不会持续存在(Phonegap)
我似乎对 localStorage 和 Phonegap 有疑问。据我了解,移动 Safari 应该保留本地存储,无论该应用程序是否在内存中。由于某种原因,当我从内存中清除应用程序并重新启动它时,本地存储缓存消失了(我可以确认它实际上正在设置数据)。
有什么想法吗?
编辑:弄清楚了。 这根本不是 localStorage 的问题。当应用程序退出时,商店确实仍然存在。该问题是由于 Phonegap 网络回调在 jQuery 文档准备好之后发生的。
这就是我修复它的方法:
function onDeviceReady() {
navigator.network.isReachable("google.com", reachableCallback, {});
}
// Check network status
function reachableCallback(reachability) {
// There is no consistency on the format of reachability
var networkState = reachability.code || reachability;
var states = {};
states[NetworkStatus.NOT_REACHABLE] = 'No network connection';
states[NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK] = 'Carrier data connection';
states[NetworkStatus.REACHABLE_VIA_WIFI_NETWORK] = 'WiFi connection';
if (networkState != 0) online = true;
}
////////// Checking navigator.onLine before document ready is key ///////////
var online = navigator.onLine || false;
$(document).ready(function() {
$(document).bind('deviceready', function(){
onDeviceReady()
})
})
I seem to have an issue with localStorage and Phonegap. From what I understand mobile safari should keep localstorage regardless if that app is in memory or not. For some reason when I clear my app from memory and restart it, the localstorage cache is gone (I can confirm that it is actually setting the data though).
Any ideas?
EDIT: Figured it out.
It's not an issue with localStorage at all. Store does persist when the app is quit. The problem was due to the Phonegap network callback happening after jQuery's document ready.
This is what I did to fix it:
function onDeviceReady() {
navigator.network.isReachable("google.com", reachableCallback, {});
}
// Check network status
function reachableCallback(reachability) {
// There is no consistency on the format of reachability
var networkState = reachability.code || reachability;
var states = {};
states[NetworkStatus.NOT_REACHABLE] = 'No network connection';
states[NetworkStatus.REACHABLE_VIA_CARRIER_DATA_NETWORK] = 'Carrier data connection';
states[NetworkStatus.REACHABLE_VIA_WIFI_NETWORK] = 'WiFi connection';
if (networkState != 0) online = true;
}
////////// Checking navigator.onLine before document ready is key ///////////
var online = navigator.onLine || false;
$(document).ready(function() {
$(document).bind('deviceready', function(){
onDeviceReady()
})
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只是把真正的答案复制到答案中,这都是从@nic aitch
想出来的。这根本不是 localStorage 的问题。当应用程序退出时,商店确实会持续存在。该问题是由于 jQuery 文档准备好后发生 Phonegap 网络回调造成的。
这就是我修复它的方法:
I'm just copying the real answer to the answer, it's all from @nic aitch
Figured it out. It's not an issue with localStorage at all. Store does persist when the app is quit. The problem was due to the Phonegap network callback happening after jQuery's document ready.
This is what I did to fix it: