Android 和 iOS 上的本地存储有多永久?

发布于 2024-12-09 11:36:02 字数 799 浏览 0 评论 0原文

当我的应用程序将数据存储在手机本地时,该存储的持久性如何?我将详细说明具体情况:

我正在使用 jQueryMobile 和 Phonegap 构建一个应用程序。它本质上是一个浏览器应用程序,但使用 Phonegap 可以让我将其打包并在应用程序商店中销售,还有其他优点。

Phonegap 提供两种存储方式,这两种方式都协调了 iO、Android、Blackberry 和其他一些操作系统的本机功能:localStorage(原始键值对)和 Web SQL 数据库。 localStorage 和 Web SQL 都是属于浏览器的存储形式。话虽如此,我无法得知数据会存储多长时间,在什么情况下会被删除,在什么情况下可能不可用等等。

例如,如果应用程序使用 localStorage 或 Web SQL 存储数据,并且用户在 Android 上切换到不同的标准浏览器,应用程序是否会使用新浏览器打开,这是否意味着存储的数据不可用?

如果用户一年没有使用该应用程序(在我的情况下这是一个现实的情况,不一定是一个糟糕的情况),数据是否会像 cookie 一样过期,或者可能被大量的数据从浏览器的存储中推出来自其他应用程序的数据?

或者数据会更早被销毁,例如: - 用户在浏览器中访问另一个网站 - 浏览器被手动关闭 - 浏览器进程被终止或死亡 - 等等

或者 localStorage 和 Web SQL 是一种只有在(在 Android 中)转到“设置”>“设置”时才删除的存储吗?应用程序并主动删除与该应用程序关联的用户数据?

感谢您的任何见解。 ol' WWW 上没有任何信息。

应用程序更新时会发生什么情况。本地和网络存储将被删除还是会保留?

When my app stores data locally on a phone, how permanent is that storage? I'll elaborate the exact situation:

I'm building an app with jQueryMobile and Phonegap. It's essentially a browser app, but using Phonegap lets me package it and sell it in the app stores, among other advantages.

Phonegap offers two ways of storage, both of which functions harmonize native functions of iOs, Android, Blackberry and some other OSs: localStorage (which is primitive key-value pairs), and a Web SQL database.
Both localStorage and web SQL are forms of storage that belong to the browser. Having said that, I can't find out how long the data will stay stored, under what circumstances it will be deleted, under what circumstances it may not be available, etc.

For example, if the app stores data with localStorage or web SQL, and the user switches to a different standard browser on their Android, will the app be opened with the new browser and does that mean that the stored data is unavailable?

If the user doesn't use the app for a year (which in my case is a realistic and not necessarily a bad scenario), will the data have expired like a cookie, or maybe been pushed out of the browser's storage by a deluge of data from other apps?

Or will the data be destroyed even earlier, such as when:
- user visits another site in the browser
- browser is manually closed
- browser process is killed or dies
- etc

Or are localStorage and web SQL the kind of storage that you only delete when (in Android) you go to Settings > Apps and actively remove the user data associated with the app?

Thanks for any insights. There's nothing informative out there on ol' WWW.

What happens in the case of app update. The local and web storage will get deleted or will it stay?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

谜泪 2024-12-16 11:36:02

让我一步一步来回答

如果应用程序使用 localStorage 或 Web SQL 存储数据,并且用户
在 Android 上切换到不同的标准浏览器后,
应用程序将使用新浏览器打开,这是否意味着存储的
数据不可用?

数据保存在浏览器的“缓存”(不完全是缓存)中,因此如果您更改浏览器,或设置默认浏览器被删除或更改,数据将会消失。

如果用户一年没有使用该应用程序(在我的例子中是
现实且不一定是糟糕的情况),数据是否会
像 cookie 一样过期,或者可能被从浏览器中推出
来自其他应用程序的大量数据存储?

不会,无论多长时间不使用,数据都会保留在那里。因此,即使您清除浏览器缓存,它仍然存在。

或者数据会更早被销毁,例如当: - user
在浏览器中访问另一个站点 - 浏览器被手动关闭 -
浏览器进程被终止或死亡 - 等等

不,数据保持正常。 :-)

或者 localStorage 和 Web SQL 是您只需要使用的存储类型吗?
当(在 Android 中)转到“设置”>“删除”时删除应用程序并主动删除
与应用程序关联的用户数据?

是的,只有当您从应用程序中手动删除数据或卸载应用程序时,数据才会消失。在所有其他情况下它将保留。

编辑:
对于iOS来说,当设备内存不足时,操作系统会删除本地存储中的数据。

Let me answer step by step

if the app stores data with localStorage or web SQL, and the user
switches to a different standard browser on their Android, will the
app be opened with the new browser and does that mean that the stored
data is unavailable?

The data is saved in the 'cache'(its not exactly the cache) of the browser, so if you change the browser, or set the settings so that the default browser is removed or changed, the data will go.

If the user doesn't use the app for a year (which in my case is a
realistic and not necessarily a bad scenario), will the data have
expired like a cookie, or maybe been pushed out of the browser's
storage by a deluge of data from other apps?

No, the data will stay there no matter for how long it is not used. So even if you clear the browser cache, it will still be there.

Or will the data be destroyed even earlier, such as when: - user
visits another site in the browser - browser is manually closed -
browser process is killed or dies - etc

No, the data stays all right. :-)

Or are localStorage and web SQL the kind of storage that you only
delete when (in Android) you go to Settings > Apps and actively remove
the user data associated with the app?

Yes, the data only goes if you either manually delete it from your app or you uninstall your app. It will stay in all other cases.

EDIT:
In the case of iOS, the OS will remove the data in the local storage when there is a shortage of memory in the device.

-残月青衣踏尘吟 2024-12-16 11:36:02

从 iOS 5.1 开始,@ghostCoder 的答案不再有效。苹果已决定将本地存储位置移动到可以随时清空的缓存文件夹中。您可以在此处跟踪此讨论:

Google关于本地存储的小组讨论

另外,此博客更详细地解释了该问题:

http:// www.marco.org/2011/10/13/ios5-caches-cleaning

可以手动将本地存储位置指向安全的 Application_Home>/Documents 地点。要确定您当前的本地存储位置,您可以使用类似以下内容:

NSString* localStorageSubdir = (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/LocalStorage";
NSString* localStoragePath = [library stringByAppendingPathComponent:localStorageSubdir];
NSString* localStorageDb = [localStoragePath stringByAppendingPathComponent:@"file__0.localstorage"];

以下代码允许您为本地存储设置另一个位置:

NSString* bundleIdentifier = [[mainBundle infoDictionary] objectForKey:@"CFBundleIdentifier"];
NSString* libraryPreferences = @"Library/Preferences";
NSString* appPlistPath = [[bundlePath stringByAppendingPathComponent:libraryPreferences]    stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", bundleIdentifier]];

NSString *value;
NSString *key = @"WebKitLocalStorageDatabasePathPreferenceKey";
value = [appPlistDict objectForKey: key];
if (![value isEqual:ourLocalStoragePath]) {
    [appPlistDict setValue:yourPreferredLocalStoragePath forKey:key];
} 

As of iOS 5.1 @ghostCoder's answer is no longer valid. Apple has decided to move the localstorage location into a cache folder which can be emptied anytime. You can track this discussion here:

Google Groups Discussion on Localstorage

Also this blog explains the problem in more detail:

http://www.marco.org/2011/10/13/ios5-caches-cleaning

It is possible to manually point your localstorage location to a safe Application_Home>/Documents location. To determine your current localstorage location you can use something like this:

NSString* localStorageSubdir = (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/LocalStorage";
NSString* localStoragePath = [library stringByAppendingPathComponent:localStorageSubdir];
NSString* localStorageDb = [localStoragePath stringByAppendingPathComponent:@"file__0.localstorage"];

The following code allows you to set another location for your localstorage:

NSString* bundleIdentifier = [[mainBundle infoDictionary] objectForKey:@"CFBundleIdentifier"];
NSString* libraryPreferences = @"Library/Preferences";
NSString* appPlistPath = [[bundlePath stringByAppendingPathComponent:libraryPreferences]    stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist", bundleIdentifier]];

NSString *value;
NSString *key = @"WebKitLocalStorageDatabasePathPreferenceKey";
value = [appPlistDict objectForKey: key];
if (![value isEqual:ourLocalStoragePath]) {
    [appPlistDict setValue:yourPreferredLocalStoragePath forKey:key];
} 
心奴独伤 2024-12-16 11:36:02

尝试使用 NativeStorage 插件。 https://www.npmjs.com/package/cordova-plugin-nativestorage

它具有设置、放置和获取功能,可实现 Android 共享首选项和 iOS NSUserDefaults 等平台功能,从而使数据存储尽可能安全。

cordova plugin add cordova-plugin-nativestorage

NativeStorage.putObject("reference_to_value",<object>, <success-callback>, <error-callback>);

NativeStorage.getObject("reference_to_value",<success-callback>, <error-callback>);

Try the NativeStorage plugin. https://www.npmjs.com/package/cordova-plugin-nativestorage.

It has set, put and get functions which implement platform capabilities like android shared preferences and iOS NSUserDefaults which makes data store as safe as allowed.

cordova plugin add cordova-plugin-nativestorage

NativeStorage.putObject("reference_to_value",<object>, <success-callback>, <error-callback>);

NativeStorage.getObject("reference_to_value",<success-callback>, <error-callback>);
荆棘i 2024-12-16 11:36:02

现在可用的一个好的解决方案是 Cordova 本机存储插件

它允许通过在 Android 中本机实现 SharedPreferences 和在 iOS 中实现 NSDefault 来在 iOS 和 Android 中保存数据,从而保证可靠性。

用法:

安装:

cordova 插件添加 cordova-plugin-nativestorage

存储值:

NativeStorage.setItem("reference_to_value",<值>,<成功回调>,<错误回调>);

检索值:

NativeStorage.getItem("reference_to_value",<成功回调>, <错误回调>);

A good solution which is available now is The Cordova Native Storage Plugin.

It allows a simple yet native persistant method to save data in iOS and Android by natively implementing SharedPreferences in Android and NSDefault in iOS to guarantee reliability.

Usage:

Installation:

cordova plugin add cordova-plugin-nativestorage

Storing values:

NativeStorage.setItem("reference_to_value",<value>,<success-callback>, <error-callback>);

Retrieving values:

NativeStorage.getItem("reference_to_value",<success-callback>, <error-callback>);
扛起拖把扫天下 2024-12-16 11:36:02

android上,默认情况下它是永久存储。即使用户更新您的应用程序,它也保持不变。

用户可以进入设置并清除缓存和数据,在这种情况下它会消失,或者如果说其中一个清理应用程序会这样做。

即使在iOS上,它也是永久存储,但不知道应用程序更新情况。但在旧版本(5.1)中,情况并非如此,在 6+ 中,他们可以通过一个由 cordova/phonegap 立即启用的标志使其永久化。

On android it's a permanent storage by default. Even if the user updates your app it stays the same.

The user can go to settings and clear cache and data in which case it goes or if say one of the clean-it apps does it.

Even on iOS it's permanent storage but don't know about app update scenario. But in older version(5.1) it wasn't and in 6+ they made that it can be made permanent through a flag which was promptly enabled by cordova/phonegap.

不再见 2024-12-16 11:36:02

我不能代表其他平台,但在我的 4.1 Android 设备上,我使用 localStorage 通过 jQuery Mobile 在本地存储一些数据,并且我发现 Android 每周左右都会在我不知情的情况下清除我的缓存。我将尝试一下 Web SQL,看看是否更好。

I can't speak for other platforms, but on my 4.1 Android device, I'm using localStorage to store some data locally with jQuery Mobile, and I've found that Android will clear my cache every week or so without my knowledge. I'm going to give Web SQL a try and see if that's better.

软糯酥胸 2024-12-16 11:36:02

在性能、安全性和可靠性方面,避免使用本地存储并使用 Cordova-sqlite-storage

一些注意事项:

localStorage 的优点是易于使用,但它以同步方式工作。这意味着它可能会锁定 UI 线程并导致性能下降。

localStorage 的存储量有限,通常为 5mb。

localStorage 可以随时被操作系统(android、ios)擦除。如果您关心数据持久性,这确实很严重。

另一方面:

Cordova-sqlite-storage 是异步的,它不会阻塞 UI 线程,它将您的数据保存在本机设备存储上,存储量为不限于此,操作系统不会删除您的任何数据,除非您卸载该应用程序。

It would be better in terms of performance, security and reliability to AVOID using local storage and use Cordova-sqlite-storage instead.

Some notes:

localStorage has the advantage of being easy to use, but it works in a synchronous way. This means that it can lock the UI thread and cause slow performance.

localStorage has a limited amount of storage, usually its 5mb.

localStorage can be wiped out by the OS (android,ios) at any time. This is really serious if you care about data persistance.

On the other hand:

Cordova-sqlite-storage is asynchronous and it will not block the UI thread, it saves your data on the native device storage, the amount of storage is not limited, the OS will not delete any of your data, unless you uninstall the app.

能否归途做我良人 2024-12-16 11:36:02

我在网上找到了这个 ng-persist

将数据存储在移动设备上(使用 cordova),即使
用户重新安装应用

安装

bower install ng-persist ngstorage --save

$persist 注入您的控制器

.controller('MyCtrl', function($persist) {

    $persist
        .set(namespace, key, val)
        .then(function () {
            // saved
        });

    // read
    $persist
        .get(namespace, key, fallback)
        .then(function (val) {
            // val is either the value, if exists, or the fallback
        });

    // delete
    $persist
        .remove(namespace, key)
        .then(function () {
            // removed
        });

});

I found this online ng-persist

Store data on mobile devices (using cordova) that persists even if the
user reinstalls the app

Install

bower install ng-persist ngstorage --save

Inject $persist into your controller

.controller('MyCtrl', function($persist) {

    $persist
        .set(namespace, key, val)
        .then(function () {
            // saved
        });

    // read
    $persist
        .get(namespace, key, fallback)
        .then(function (val) {
            // val is either the value, if exists, or the fallback
        });

    // delete
    $persist
        .remove(namespace, key)
        .then(function () {
            // removed
        });

});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文