gpt4 book ai didi

sqlite - Ionic 2 存储模块说明

转载 作者:行者123 更新时间:2023-12-03 15:53:57 24 4
gpt4 key购买 nike

ionic 2 存储方面存在很多混淆。新的 ionic 版本发生了很多变化,因为 Storage 被移到了 @ionic/strage 。我是 Ionic 的新手,所以有些事情让我感到困惑。我有网络开发背景。从文档中,

A simple key-value Storage module for Ionic apps based on LocalForage, with out-of-the-box support for SQLite. This utility makes it easy to use the best storage engine available without having to interact with it directly. Currently the ordering is SQLite, IndexedDB, WebSQL, and LocalStorage.

安装

npm install @ionic/storage

如果您想使用 SQLite 作为存储引擎,请安装 SQLite 插件(仅在模拟器或设备上运行时有效):

cordova plugin add cordova-sqlite-storage --save

我想知道的是,当我在浏览器中运行它时会发生什么?它在哪里存储数据?如果我不使用 cordova-sqlite-storage 会发生什么?那么它在哪里存储数据呢?

Ionic 还原生支持 SQLite 插件,将数据存储在 SQLite 数据库中。

从 'ionic-native' 导入 { SQLite }

除了可以回退到 IndexedDB、WebSQL 和 LocalStorage 之外,它与 Storage 有何不同?

我希望我的想法是在正确的方向上。关于这些模块如何工作的明确答案将非常有帮助。

最佳答案

Ionic Storage 是第一个在编写时考虑到适当的网络回退的模块。

One near-term goal we have with Ionic is enabling devs to build 99% of their app in the browser. It's a much faster workflow. This means support for native plugins that have web fallbacks, as well as better mocking for ones that don't. - Max Lynch on Twitter

在运行时默认情况下,@ionic/storage 将以这种方式优先存储方法:

When running in a native app context, Storage will prioritize using SQLite, as it's one of the most stable and widely used file-based databases, and avoids some of the pitfalls of things like localstorage and IndexedDB, such as the OS deciding to clear out such data in low disk-space situations.

When running in the web or as a Progressive Web App, Storage will attempt to use IndexedDB, WebSQL, and localstorage, in that order. - Official Documentation

因此,当您的应用程序在浏览器(或没有 SQLite 插件的设备)中运行时,它会检测到 SQLite 不可用,并将改用 IndexedDB/WebSQL。

除了可以回退到 IndexedDB、WebSQL 和 LocalStorage,它与 Storage 有何不同?

SQLite 插件为您提供了对 SQLite 数据库的低级访问,这意味着您必须关心创建/更新您的模式,以及编写查询。

@ionic/storage 是一个包装器,它抽象出 LocalForgeSQLite 的差异,并提供一个简单、统一的 API 来存储 key /值对。

它还负责序列化/反序列化您的对象。

关于sqlite - Ionic 2 存储模块说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39943224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com