简体   繁体   English

在Android中存储数据的安全方法是什么?

[英]What's a safe way to store data in Android?

I've been using singletons to store data that is loaded when my application started. 我一直在使用单例存储应用程序启动时加载的数据。 This data is used throughout the lifetime of my app, but doesn't need to be persisted for future launches. 这些数据会在我的应用的整个生命周期内使用,但在以后的发布中不需要保留。 I've started hitting the issue though that my singletons are garbage collected and their data is wiped. 尽管我的单例被垃圾收集并且其数据已擦除,但我已经开始解决这个问题。 Does the singleton pattern just not make any sense on Android? 单例模式在Android上是否没有任何意义? Using local storage or a local database seems like overkill, but I'm not sure what else to do to guarantee my data is safe for the lifetime of my app. 使用本地存储或本地数据库似乎有些矫kill过正,但是我不确定要采取其他措施来确保我的数据在应用程序的生命周期内是安全的。

Thanks! 谢谢!

I've started hitting the issue though that my singletons are garbage collected and their data is wiped. 尽管我的单例被垃圾收集并且其数据已擦除,但我已经开始解决这个问题。

No, they are not. 不,他们不是。 By definition, a singleton (static data member) cannot be garbage collected. 根据定义,单例(静态数据成员)不能被垃圾收集。

Most likely, what you are seeing is that your process is being terminated . 您最有可能看到的是您的进程正在终止

Does the singleton pattern just not make any sense on Android? 单例模式在Android上是否没有任何意义?

As a caching mechanism, it is fine. 作为缓存机制,这很好。

I'm not sure what else to do to guarantee my data is safe for the lifetime of my app 我不确定要采取其他措施来确保我的数据在应用的生命周期内是安全的

A static data member is "safe for the lifetime" of your process . 静态数据成员对您的过程 “一生都是安全的”。 If you want data to survive process termination, you will need to store that data in a database, SharedPreferences , or some other sort of file. 如果您希望数据在过程终止后仍然存在,则需要将该数据存储在数据库, SharedPreferences或其他某种文件中。

Are you holding onto a reference to the singletons in the Application ( http://developer.android.com/reference/android/app/Application.html ) level of the app? 您是否在应用程序的应用程序( http://developer.android.com/reference/android/app/Application.html )级别中引用单例? That should keep them persistent for the most part (unless they're byte[] or something else huge). 那应该使它们在大多数情况下保持持久(除非它们是byte []或其他巨大的东西)。

The safest bet, however, is to assume that data will be garbage collected and structure your code with that assumption. 但是,最安全的选择是假设将对数据进行垃圾收集,并以此假设为基础构建代码。 If it's something from the web, define an asynchronous callback to load the data and then populate your views. 如果来自网络,则定义一个异步回调以加载数据,然后填充视图。 If it's something local that is not user-generated, use the async to grab it from disk. 如果它不是用户生成的本地内容,请使用异步从磁盘中获取它。 If it's user generated, you might need to set up a database (which you can empty when you restart the app) to keep track of data. 如果是用户生成的,则可能需要设置一个数据库(在重新启动应用程序时可以将其清空)以跟踪数据。

But, if you haven't, tried keeping references to your singletons in the Application first. 但是,如果您还没有尝试过,请先尝试在应用程序中保留对单例的引用。 Brief intro here: http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/ 此处的简要介绍: http : //www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM