离子项目localStorage在添加人行横道后停止工作

时间:2015-05-20 20:55:58

标签: cordova ionic-framework crosswalk-runtime

我有一个使用localStorage的离子项目,已经工作了好几个月,但是在我添加了人行横道之后,localStorage不再有效了。奇怪的是,我使用ionic start todo-list sidemenu创建了另一个应用程序,并使用localStorage创建了一个简单的待办事项列表并且工作正常。

可能问题在于我的另一个应用程序中的某些内容与人行横道相结合,这些内容存在冲突并破坏了localStorage

我知道我说的是模糊的,但也许有人会遇到同样的问题。

this link

  

Crosswalk WebView与System WebView分开存储数据(IndexedDB,LocalStorage等)

我这样使用:

window.localStorage.setItem('foo', 'bar');
window.localStorage.getItem('foo');

上面的示例中,getItem会显示bar值,但如果我关闭应用并在应用的其他部分执行getItem('foo'),则返回{{1} }}

问题恰好发生在Android设备上(经测试4.1,4.3和4.4),网络浏览器正常运行。

对于记录,这是项目中安装的插件:

null

1 个答案:

答案 0 :(得分:1)

我有同样的问题(使用cordova-android 4.0和Crosswalk 13.42.319.11)。在添加Crosswalk之前,localStorage按预期工作。添加Crosswalk后,如果应用程序卸载并重新启动,则不会保留添加到本地存储的任何新项目。

通过一个消除过程,我在我们的应用程序中发现了以下似乎触发问题的代码:

// The purpose of this code is to check whether Safari browser is in "Private" browsing mode,
// in which case the localStorage API is available but throws an exception when setItem() is called:
// "QuotaExceededError: DOM Exception 22: An attempt was made to add something to storage that exceeded the quota."

function testLocalStorageIsSupported() {
    try {
        if (window.localStorage && window.localStorage.setItem && window.localStorage.getItem && window.localStorage.removeItem) {
            // Store an arbitrary value
            var storedValue = (new Date().valueOf()).toString();
            window.localStorage.setItem("__LocalStorageIsSupported__", storedValue);

            var loadedValue = window.localStorage.getItem("__LocalStorageIsSupported__");
            window.localStorage.removeItem("__LocalStorageIsSupported__");

            // If loaded value matches the stored value then consider localStorage to be supported.
            return (storedValue == loadedValue);
        }

        return false;
    } catch (e) {
        console.log("LocalStorage: Caught an error testing availability of localstorage: " + e);
        return false;
    }
}

收到deviceready事件后调用此代码。从应用程序中删除此代码解决了我的问题。

我将问题代码缩减为一行:

window.localStorage.setItem("__LocalStorageIsSupported__", (new Date().valueOf()).toString())

我做了一些进一步的测试,创建了一个空白的cordova-android 4.0应用程序,并添加了Crosswalk。最初localStorage可以正常工作。将上面一行代码添加到deviceready事件的回调中后,localStorage停止了持久化。