为什么我不能在给定的会话中将超过2个新条目保存到LocalStorage密钥?

时间:2015-05-03 21:17:41

标签: javascript html5 cordova local-storage

我有一个脚本,它当前迭代数组中的某些对象,并从该对象的“url”属性下载文件。下载完成后,我将该对象添加到其他数组并将其持久保存到localstorage。

假设我有50个文件需要下载。如果我启动应用程序并开始下载它们并处理超过2.只有2个新对象被添加到localstorage。甚至以为其他文件实际上已经下载了。

var App = {
    settings: {
        apiKey: null,
        username: null,
        websites: [],
        activeWebsite: null,
        validManifest: false,
        manifest: {}
    },
    manifestChanges: {
        raw: {},
        totalFileChanges: 0,
        totalFilesProcessed: 0,
        totalFilesToAdd: 0,
        totalFilesAdded: 0,
        totalFilesToReplace: 0,
        totalFilesReplaces: 0,
        totalFilesToRemove: 0,
        totalFilesRemoved: 0
    },
}

然后在App中我有一个调用这段代码的函数

StorageManager.downloadFile(file.remoteUrl,function(progressEvent){
    var filePercent = 100;
    if (progressEvent.lengthComputable) {
        filePercent = progressEvent.loaded / progressEvent.total * 100
    }
    App.loadPage('progress',{
        'heading': 'Downloading New Files',
        'mainTitle': 'Total Progress ('+App.manifestChanges.totalFilesAdded+'/'+App.manifestChanges.totalFilesToAdd+')',
        'mainPercent':  Math.floor(App.manifestChanges.totalFilesAdded / App.manifestChanges.totalFilesToAdd * 100),
        'secondaryTitle': 'File Progress: '+property,
        'secondaryPercent': Math.floor(filePercent)
    });
},function(){
    console.log('saved out: '+property);
    App.manifestChanges.totalFilesAdded++;
    App.manifestChanges.raw.add[property]['finished'] = true;
    $.extend(true, App.settings, StorageManager.getSettings());
    App.settings.manifest[property] = file;
    console.log(JSON.stringify(App.settings.manifest[property]));
    setTimeout(function(){
        if(StorageManager.saveSettings(App.settings)){
            App.loadPage('progress',{
                'heading': 'Downloading New Files',
                'mainTitle': 'Total Progress ('+App.manifestChanges.totalFilesAdded+'/'+App.manifestChanges.totalFilesToAdd+')',
                'mainPercent':  Math.floor(App.manifestChanges.totalFilesAdded / App.manifestChanges.totalFilesToAdd * 100)
            });
            App.handleManifestAdd(cb);
        }
        else{
            alert('Failed to update manifest after saving: '+property);
        }
    },500);
})

StorageManager:

var StorageManager = {
    getSettings: function () {
        console.log('getting settings');
        if (typeof window.localStorage !== 'undefined') {
            return JSON.parse(localStorage.getItem('AppSaSettings'));
        }
        else {
            console.log('local storage not defined');
            return {};
        }
    },
    saveSettings: function (newSettings) {
        if (typeof window.localStorage !== 'undefined') {
            localStorage.setItem('AppSaSettings', JSON.stringify(newSettings));
            return true;
        }
        else {
            return false;
        }
    },
    downloadFile: function (url, progressCb, finishCb) {
        var fileTransfer = new FileTransfer();
        var uri = encodeURI(url);
        var urlParts = uri.split('/');
        fileTransfer.download(
            uri,
            StorageManager.downloadsFolder + urlParts[urlParts.length -     1],
            function (entry) {
                finishCb(entry.toURL());
            },
            function (error) {
                console.log("download error source " + error.source);
                console.log("download error target " + error.target);
                console.log("upload error code" + error.code);
                finishCb(false);
            },
            true
        );
        fileTransfer.onprogress = progressCb;
    }
}

超时是在那里看看它是否是异步问题。我一直在尝试在太阳下我能想到的一切,试图弄清楚为什么它不会节省更多的2次更新。任何帮助表示赞赏。

0 个答案:

没有答案