简体   繁体   English

仅部分检索到的localStorage数据

[英]Only partially retrieved localStorage data

I'm currently working on a mobile app using Phonegap 1.4.1. 我目前正在使用Phonegap 1.4.1开发移动应用程序。 On iOS (currently using 5.1) the app won't load all the data from localStorage. 在iOS(当前使用5.1)上,该应用不会从localStorage加载所有数据。

The first time the app is used I set a flag in localStorage like so: 第一次使用该应用程序时,我在localStorage中设置了一个标志,如下所示:

window.localStorage.setItem("myFlag", "true");

I also set items "ItemA", "ItemB", and "ItemC" in the same way. 我还以相同的方式设置了项目“ ItemA”,“ ItemB”和“ ItemC”。 Each of these key/value pairs are set at different times during the use of the app, so there's no defined order in which they are set. 这些键/值对中的每一个在应用程序的使用过程中都在不同的时间设置,因此设置时没有定义的顺序。

My problem is this: 我的问题是这样的:

When I reinstall the app, without deleting the existing copy first, the app no longer sees "MyFlag", "ItemB" or "ItemC" - it does see "ItemA". 当我重新安装应用程序,而不先删除现有副本,应用程序不再看到“MyFlag”,“I​​temB”或“ItemC” -它确实看到“意达”。 Using window.localStorage.length returns 1, when it should return 4. I have exported the app's data files and examined the file__0.localstorage file with SQLite Inspector (app from the app store) and it shows all four key/value pairs. 使用window.localStorage.length返回1,应返回4。我已经导出了应用程序的数据文件,并使用SQLite Inspector(来自应用程序商店的应用程序)检查了file__0.localstorage文件,它显示了所有四个键/值对。

Since iOS 5.1+ has moved the localstorage to the Cache folder I have implemented the backing up of the file__0.localstorage file as described here . 由于iOS版5.1+已经移动到的localStorage的缓存文件夹,我已经实现了file__0.localstorage文件所描述的备份这里 Basically, what that does is copy the localstorage db to the Documents folder on app pause and exit, and back to the Cache folder on execution of webViewDidStartLoad. 基本上,要做的是在应用程序暂停并退出时将localstorage db复制到Documents文件夹,并在执行webViewDidStartLoad时将其复制到Cache文件夹。 This part is working fine, so I don't think that procedure is causing the problem (the symptom of the problem existed in the app before I added that procedure). 这部分工作正常,所以我不认为该过程引起了问题(在我添加该过程之前,该应用程序中存在问题的症状)。

This turned out to be a result of the Cordova (Phonegap) bug reported here . 原来,这是此处报告的Cordova(Phonegap)错误的结果 So it was a combination of Apple moving the location of the file__0.localstorage file to the Cache directory and an Apple bug where the Bundle ID for apps is changed when upgrading to a new version of an app and a couple of .plist file entries weren't updated to reflect the new Bundle ID. 因此,这是苹果将file__0.localstorage文件的位置移至Cache目录和一个苹果错误的结合,苹果错误是在升级到新版本的应用程序时更改了应用程序的捆绑程序ID,并且没有几个.plist文件条目进行了更新,以反映新的Bundle ID。

The proposed fix, posted as an attachment here didn't quite work out-of-the-box, as the files included referenced CDVInvokedUrlCommand which referenced JSONKit. 提议的修复程序(作为附件发布) 开箱即用时效果不佳,因为包含的文件引用了CDVInvokedUrlCommand,引用了JSONKit。 So downloading the latest CDVInvokedUrlCommand.h and CDVInvokedUrlCommand.m as well as JSONKit.h and JSONKit.m (all from the Cordova GitHub repo) was also necessary. 因此,也有必要下载最新的CDVInvokedUrlCommand.h和CDVInvokedUrlCommand.m以及JSONKit.h和JSONKit.m(均来自Cordova GitHub存储库)。 The code in the README.txt file of the Phongap 1.4.1 version of the proposed fix also included an error. 提议的修补程序的Phongap 1.4.1版本的README.txt文件中的代码也包含一个错误。 It said: 它说:

6) In your app's "AppDelegate.m", replace your "webViewDidStartLoad" function with this: 6)在应用程序的“ AppDelegate.m”中,将“ webViewDidStartLoad”函数替换为:

    - (void) webViewDidStartLoad:(UIWebView *)theWebView 
    {
        static CDVLocalStorage* localStorage = nil;
        if (localStorage == nil) {
            localStorage = [[CDVLocalStorage alloc] initWithWebView:theWebView];   
            [localStorage restore:nil withDict:nil];
        }

        return [ super webViewDidStartLoad:theWebView ];
    }

But it should have said to add the body of the above method to the method you already have with this signature - and the line [localStorage restore:nil withDict:nil]; 但是应该已经说过,将上述方法的主体添加到您已经具有此签名的方法中了-行[localStorage restore:nil withDict:nil]; should be changed to [localStorage restore:nil]; 应该更改为[localStorage restore:nil]; , as the former method signature doesn't exist anywhere. ,因为以前的方法签名在任何地方都不存在。

Of course, the proper imports also had to be added to AppDelegate.m. 当然,正确的导入也必须添加到AppDelegate.m中。


Upgrading to the latest version of Phonegap would be the simplest solution for those who are able to do so. 对于有能力的人来说,升级到最新版本的Phonegap将是最简单的解决方案。 However, we are using the (relatively suddenly) ancient version 1.4.1 and a 3rd party plugin which isn't compatible with newer versions of Phonegap yet - so this fix was the best fit for us. 但是,我们正在使用(相对突然)古老的1.4.1版本和一个第三方插件,该插件与新版本的Phonegap尚不兼容-因此此修复最适合我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM