简体   繁体   中英

Only partially retrieved localStorage data

I'm currently working on a mobile app using Phonegap 1.4.1. On iOS (currently using 5.1) the app won't load all the data from localStorage.

The first time the app is used I set a flag in localStorage like so:

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

I also set items "ItemA", "ItemB", and "ItemC" in the same way. 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". 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.

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

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. 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. The code in the README.txt file of the Phongap 1.4.1 version of the proposed fix also included an error. It said:

6) In your app's "AppDelegate.m", replace your "webViewDidStartLoad" function with this:

    - (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]; should be changed to [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.


Upgrading to the latest version of Phonegap would be the simplest solution for those who are able to do so. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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