Android 和 iOS 上的本地存储有多永久?
当我的应用程序将数据存储在手机本地时,该存储的持久性如何?我将详细说明具体情况:
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
让我一步一步来回答
数据保存在浏览器的“缓存”(不完全是缓存)中,因此如果您更改浏览器,或设置默认浏览器被删除或更改,数据将会消失。
不会,无论多长时间不使用,数据都会保留在那里。因此,即使您清除浏览器缓存,它仍然存在。
不,数据保持正常。 :-)
是的,只有当您从应用程序中手动删除数据或卸载应用程序时,数据才会消失。在所有其他情况下它将保留。
编辑:
对于iOS来说,当设备内存不足时,操作系统会删除本地存储中的数据。
Let me answer step by step
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.
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.
No, the data stays all right. :-)
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.
从 iOS 5.1 开始,@ghostCoder 的答案不再有效。苹果已决定将本地存储位置移动到可以随时清空的缓存文件夹中。您可以在此处跟踪此讨论:
Google关于本地存储的小组讨论
另外,此博客更详细地解释了该问题:
http:// www.marco.org/2011/10/13/ios5-caches-cleaning
可以手动将本地存储位置指向安全的 Application_Home>/Documents 地点。要确定您当前的本地存储位置,您可以使用类似以下内容:
以下代码允许您为本地存储设置另一个位置:
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:
The following code allows you to set another location for your localstorage:
尝试使用 NativeStorage 插件。 https://www.npmjs.com/package/cordova-plugin-nativestorage 。
它具有设置、放置和获取功能,可实现 Android 共享首选项和 iOS NSUserDefaults 等平台功能,从而使数据存储尽可能安全。
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 本机存储插件。
它允许通过在 Android 中本机实现 SharedPreferences 和在 iOS 中实现 NSDefault 来在 iOS 和 Android 中保存数据,从而保证可靠性。
用法:
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:
在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.
我不能代表其他平台,但在我的 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.
在性能、安全性和可靠性方面,避免使用本地存储并使用
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.我在网上找到了这个
ng-persist
安装
bower install ng-persist ngstorage --save
将
$persist
注入您的控制器I found this online
ng-persist
Install
bower install ng-persist ngstorage --save
Inject
$persist
into your controller