当应用程序从内存中退出时,localStorage不会持续存在(Phonegap) [英] localStorage doesn't persist when the app is quit from memory (Phonegap)

查看:100
本文介绍了当应用程序从内存中退出时,localStorage不会持续存在(Phonegap)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎对localStorage和Phonegap有问题.据我了解,无论该应用是否在内存中,移动设备的野生动物园都应保留本地存储.由于某种原因,当我从内存中清除我的应用程序并重新启动它时,本地存储缓存不见了(我可以确认它实际上是在设置数据).

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).

有什么想法吗?

找到答案. 这根本不是localStorage的问题.退出应用后,存储 does 仍然存在.问题是由于 jQuery文档准备就绪后 发生了Phonegap网络回调.

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()
  })
})

推荐答案

我只是将真正的答案复制到答案中,这全都来自@nic aitch

弄清楚了.这根本不是localStorage的问题.退出应用后,商店确实存在.问题是由于jQuery文档准备就绪后发生的Phonegap网络回调.

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()
  })
})

这篇关于当应用程序从内存中退出时,localStorage不会持续存在(Phonegap)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆