简体   繁体   English

何时在Android上使用本地存储,推荐使用哪一个?

[英]When to use Local storage on Android, and which one is recommended?

I am developing an application where I need to handle some data during the Application session. 我正在开发一个应用程序,需要在应用程序会话期间处理一些数据。 I do NOT need the data after application is killed or user manually logs out of the application. 应用程序被杀死或用户手动注销应用程序后,我不需要数据。

I am using POJOs to save the data. 我正在使用POJO保存数据。 POJOs are like Accounts, Users, Campuses, etc. I get all these data in the form of JSON from REST API calls. POJO类似于Accounts,Users,Campus等。我从REST API调用中以JSON的形式获取所有这些数据。

As I am fairly new Android development, I am trying to understand which storage approach is recommended in this scenario. 由于我是相当新的Android开发人员,因此我试图了解在这种情况下建议使用哪种存储方法。 (SQLite, SharedPreferences, ApplicationContext, Bundles, ArrayList, Maps, etc) (SQLite,SharedPreferences,ApplicationContext,Bundles,ArrayList,Map等)

Performance is important and searches are performed on this data during the user session. 性能很重要,在用户会话期间将对此数据执行搜索。

I would need to construct JSON objects often and post back to API. 我将需要经常构造JSON对象并将其发布回API。

Any ideas? 有任何想法吗?

Place data on Application 将数据放在应用程序上

if you don't need to persist your data and its lifecycle is short you might want to create some subclass of Application to hold it. 如果您不需要持久化数据并且数据生命周期短,则可能需要创建Application的某个子类来保存它。 It is the way of having some in-memory global data in the Android Platform - > How-to? 这是在Android平台中存储一些内存中全局数据的方法-> 操作方法?

Place data on in memory SQLite table/database 将数据放在内存SQLite表/数据库中

Or you might want some in-memory SQLite tables if this will help you to query the data. 或者,如果这可以帮助您查询数据,则可能需要一些内存中的SQLite表。 In memory tables are also possible in Android. 在内存表中也可以在Android中使用。 How-to? 如何?

Considerations 注意事项

First method may give you the best performance. 第一种方法可能会为您提供最佳性能。 All data is on memory, so it is easy to access. 所有数据都在内存中,因此很容易访问。 But if the data has a very complex structure and queries are complex, you might wanna use SQLite to make querying data easier data. 但是,如果数据具有非常复杂的结构,并且查询也很复杂,那么您可能想使用SQLite来使数据查询变得更容易。 Performance will depend on how much is complex your data and queries. 性能将取决于您的数据和查询的复杂程度。 Performance will still be quite good because things are on memory. 由于事情还在记忆中,因此性能仍然会相当不错。

But you must be careful. 但是您必须小心。 If you end up using too much memory you are going to make your application more likely to be killed by Android framework. 如果最终使用过多的内存,您将使您的应用程序更有可能被Android框架杀死。 And your data will be lost. 您的数据将丢失。 (5MB may be okay, 10MB might be too much. You will have to test to see what happens.) (5MB可能没问题,10MB可能太多了。您将不得不进行测试以查看会发生什么。)

If memory pressure is high then you should offload to disk. 如果内存压力很高,则应卸载到磁盘。 Performance will be worse, specially when storage is almost full.In that case access may be pretty slow. 性能会变差,特别是在存储空间即将满的情况下。访问速度可能会很慢。 The simplest way to store and retrieve data is SharedPreferences. 存储和检索数据的最简单方法是SharedPreferences。 You could write you JSON as a String preference. 您可以将JSON写为String首选项。 Again if data is too complex and querying it like a database is handy, you could use SQLite. 同样,如果数据太复杂并且像数据库一样方便查询,则可以使用SQLite。

SQLite is good option if you need random access and querying of your data, but you will have to implement CRUD or some kind of SQL databindung yourself. 如果需要随机访问和查询数据,SQLite是一个不错的选择,但是您必须自己实现CRUD或某种SQL databindung。

If you need all your data in one piece, you may store them as JSON on local storage and then parse it ( I wrote small databindung library making it easier: https://github.com/ko5tik/jsonserializer ) 如果您需要将所有数据打包在一起,则可以将它们作为JSON存储在本地存储中,然后进行解析(我编写了小的databindung库,使其更容易: https : //github.com/ko5tik/jsonserializer

If your data is kind of preferences aith limited amount, you may truy preferences storage ( I also wrote small library for this: https://github.com/ko5tik/andject will allow you to marshall and ummarshall preferences easily into objects ) 如果您的数据是有限数量的首选项,那么您可能会伪造首选项存储(我也为此编写了一个小型库: https : //github.com/ko5tik/andject将允许您轻松地将首选项编组和ummarshall到对象中)

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

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