【问题标题】:How persistent localStorage with Cordova 3.6.x?如何使用 Cordova 3.6.x 持久化 localStorage?
【发布时间】:2015-05-28 19:25:16
【问题描述】:

我很想知道 localStorage 在 Cordova 3.6.x 上的持久性如何?

存储应用程序的配置是否足够好?

如果我从 AppStore/GooglePlay 更新应用程序会发生什么,它是否仍然保留收集的用户数据?

如果我想要一个带有cordova应用程序的持久和预填充数据,你会建议我使用什么cordova插件?

提前致谢。

【问题讨论】:

  • 我看不出这个问题应该得到-1的理由。谁能解释为什么这是一个不好的问题?
  • @AbeFehr 我也不知道。人们认为很难问问题,因为它不会给我简单的分数

标签: cordova persistence local-storage phonegap-plugins


【解决方案1】:

我看过很多关于本地存储的链接,遗憾的是还没有找到明确的答案。我可以建议你一个数据库,SQLite wrapper plugin

这很简单,而且效果很好。希望它能满足您的所有要求,包括预先填充的数据库。

几个例子:

//Pre-populated database

//For Android & iOS (only): put the database file in the www directory and open the database like:

var db = window.sqlitePlugin.openDatabase({name: "my.db", createFromLocation: 1});

用法

// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
function onDeviceReady() {
  var db = window.sqlitePlugin.openDatabase({name: "my.db"});

  db.transaction(function(tx) {
    tx.executeSql('DROP TABLE IF EXISTS test_table');
    tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');

    // demonstrate PRAGMA:
    db.executeSql("pragma table_info (test_table);", [], function(res) {
      console.log("PRAGMA res: " + JSON.stringify(res));
    });

    tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
      console.log("insertId: " + res.insertId + " -- probably 1");
      console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");

      db.transaction(function(tx) {
        tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
          console.log("res.rows.length: " + res.rows.length + " -- should be 1");
          console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
        });
      });

    }, function(e) {
      console.log("ERROR: " + e.message);
    });
  });
}

您可以从文档中了解所有内容。

【讨论】:

    【解决方案2】:

    如果您不违反任何限制,我认为不会有问题。看看那个关于限制的帖子:HTML5 localStorage size limit for subdomains

    还有iOS 7 webview and localStorage persistence

    您可以选择https://github.com/jcfant/cacheJS,或者如果您使用 Ionic 或 Angular:

    【讨论】:

    • 很多链接。能否请您简短地告诉我“如果有人更新应用程序,本地存储是否会持续存在?”
    • 这个问题其实很有趣。我认为这是我最关心的问题。
    • 是的,它确实在更新中幸存下来——我可以从我自己的经历中看出。如果您不确定,this guy 也是这样说的 ;)
    • @MathiasMaciossek 这不是他所说的我所知道的问题。这实际上是我的问题的证明。
    • localStorage、Web SQL、IndexDB 在 iOS 和 Android 的更新中都是持久的。但是,当您完全删除应用程序时,它们会被清除。
    【解决方案3】:

    您可以使用cordova-plugin-nativestorage

    用法:NativeStorage.setItem("key", "value", success, error);

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 2020-11-11
      • 2012-06-27
      • 2018-06-22
      • 2021-05-20
      • 1970-01-01
      • 2020-10-11
      • 2012-04-14
      相关资源
      最近更新 更多