简体   繁体   English

文件或数据库? - 在Android设备上保存对象的最佳实践

[英]File or Database? - Best Practice to save Objects on Android-Device

I'm building an android application in java where I define some Objects like "user" or "playlist" etc.. 我正在java中构建一个android应用程序,我在其中定义了一些像“user”或“playlist”等对象。

How to save these self-defined objects on the device for later access? 如何在设备上保存这些自定义对象以供以后访问?

    Gson gson = new Gson();
    String json = gson.toJson(user);

I can parse the objects via GSON to a JSONObject or a JSONArray. 我可以通过GSON将对象解析为JSONObject或JSONArray。 Now I have two options to save the strings: In a Database or a File. 现在我有两个选项来保存字符串:在数据库或文件中。 I know how to use the android database-classes and the filewriter/reader classes, but what is the best practice with regards to performance, accessibility and especially to simplicity? 我知道如何使用android数据库类和文件编写器/阅读器类,但在性能,可访问性,尤其是简单性方面,最佳做法是什么?

The database scales well, the database engine solves the problem of memory management, the database has good performance (when used properly), the database engine allows you to run complex queries without writing a lot of Java code, the database engine allows you to insert or delete rows without writing an entire file, the database is natively supported in Android's widgets. 数据库扩展性好,数据库引擎解决了内存管​​理问题,数据库具有良好的性能(使用得当),数据库引擎允许您运行复杂查询而无需编写大量Java代码,数据库引擎允许您插入或删除行而不编写整个文件,Android的小部件本机支持该数据库。 Remember that you're limited how much memory your app can use, so if your JSON files get very big, you won't be able to load them entirely into memory. 请记住,您的应用程序可以使用的内存有限,因此如果您的JSON文件变得非常大,您将无法将它们完全加载到内存中。

It really depends on your use case . 这真的取决于你的用例

If your application data doesn't change a lot (think a conference app that is used once or twice a year), then you can use a ContentProvider + CursorAdapters which will make your life easier. 如果您的应用程序数据没有太大变化 (想想一年一次或两次使用的会议应用程序),那么您可以使用ContentProvider + CursorAdapters,这将使您的生活更轻松。

If your application data changes a lot (think Gmail, where as much as hundreds of email can pop-up every day, or a news feed, or a social networking app like Google+), then a database could probably make your life worse. 如果您的应用程序数据发生了很大变化 (想想Gmail,每天可以弹出数百封电子邮件,或者是新闻Feed,或像Google+这样的社交网络应用程序),那么数据库可能会让您的生活变得更糟。 You should use in this case a 3rd party caching system, like Google Volley or RoboSpice, etc. which handles all the caching of JSON objects for you and all the concurrency problems that appear, etc. 在这种情况下,您应该使用第三方缓存系统,如Google Volley或RoboSpice等,它们可以为您处理所有JSON对象的缓存以及出现的所有并发问题等。

The SQLite db built in to Android would be a good solution for caching large amounts of information. 内置于Android的SQLite数据库是缓存大量信息的好方法。 For instance, if you are constantly retrieving data from the cloud and then planning on storing it, you should use a db. 例如,如果您经常从云中检索数据,然后计划存储它,则应使用数据库。 Dbs are built for handling large amounts of data because they include indexing capabilities. Dbs是为处理大量数据而构建的,因为它们包含索引功能。

An example of caching JSON data with SQLite db can be found here: Caching downloaded JSON data to SQLite database - is it a good idea? 可以在此处找到使用SQLite db缓存JSON数据的示例:将下载的JSON数据缓存到SQLite数据库 - 这是一个好主意吗?

Use a database . 使用database Your JSON can easily be parsed and read in to the database. 您的JSON可以轻松解析并读入数据库。 Plus you can use SQL queries to find and manipulate the data anyway you need in the future. 此外,您可以使用SQL查询来查找和操作将来需要的数据。 A flat file will not allow you the same flexibility. 平面文件不允许您具有相同的灵活性。 Files can be useful for other applications but for JSON, broken into objects, representing things - a database makes much more sense. 文件对于其他应用程序非常有用,但对于JSON,分解为对象,表示事物 - 数据库更有意义。

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

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