当应用程序从内存中退出时,localStorage 不会持续存在(Phonegap)

发布于 2024-10-22 07:15:06 字数 1133 浏览 3 评论 0原文

我似乎对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2024-10-29 07:15:07

我只是把真正的答案复制到答案中,这都是从@nic aitch

想出来的。这根本不是 localStorage 的问题。当应用程序退出时,商店确实会持续存在。该问题是由于 jQuery 文档准备好后发生 Phonegap 网络回调造成的。

这就是我修复它的方法:

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'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:

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()
  })
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文